python - Shopify Python API:如何将产品列表序列化为 JSON?
问题描述
我想使用 Shopify Python API 获取产品列表并以 JSON 形式返回。我尝试了 .to_json() 函数
products=shopify.Product.find().to_json()
得到错误
'PaginatedCollection' object has no attribute 'to_json'
我试着做
products=shopify.Product.find()
js=json.dumps(products)
错误:
Object of type Product is not JSON serializable
如何将 Products 响应序列化为 JSON ?
解决方案
您需要遍历列表,将每个转换为 dict(to_dict() 函数)并附加到列表中。
products=shopify.Product.find()
productsJSON=[]
for product in products:
productsJSON.append(product.to_dict())
您现在可以将 productsJSON 列表作为 JSON 响应发送。
推荐阅读
- php - PHP 编译错误 *with* zts support
- python - 如何根据熊猫中现有列的值创建新列
- java - java中的final类是否与所有方法都声明为final且所有变量都声明为private的类相同
- asp.net - 如何在 ASP.NET 中将 IHttpClientFactory 添加到 NInject
- angular - Apache 问题上的 Angular 服务器端渲染,不加载诸如 runtime.js、main.js、styles.css 之类的文件
- excel - 复制行和所有代码和条件格式
- google-apps-script - 如何使用 SpreadSheetApp 和 google App 脚本将电子表格单元格的格式设置为“日期”
- java - Spring RestTemplate 给出 $InternalServerError: 500 null。同样适用于 POSTMAN 和 SMALL FILES
- pine-script - Thinkorswim 到 Tradeview
- python - 将相同的熊猫数据框存储到数据库后不相等