我是Python的新手。我试图编写一个脚本来接收JSON文件并将JSON的内容返回给API。我不使用json.dumps()
因为(至少在documentation中)JSON在发货之前没有转换为字符串。
import sys, json
from woocommerce import API
products_json = sys.argv[1]
wcapi = API(
url=SITE_URL,
consumer_key=CONSUMER_KEY,
consumer_secret=CONSUMER_SECRET,
wp_api=True,
version="wc/v2"
)
with open(products_json, 'r') as f:
products_data = json.load(f)
print(wcapi.post("products/batch", products_data).json())
这是我的JSON的内容:
{
"create": [
{
"name": "Woo Single #1",
"type": "simple",
"status": "draft",
"regular_price": "21.99",
"virtual": true,
"downloadable": true,
"downloads": [
{
"name": "Woo Single",
"file": "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/cd_4_angle.jpg"
}
],
"categories": [
{
"id": 11
},
{
"id": 13
}
]
},
{
"name": "New Premium Quality",
"type": "simple",
"status": "draft",
"regular_price": "21.99",
"description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.",
"short_description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.",
"categories": [
{
"id": 9
},
{
"id": 14
}
],
}
]
}
我得到的错误消息如下:
C:\Users\absol\Desktop>python apmarble-wcapi.py "test.json"
Traceback (most recent call last):
File "apmarble-wcapi.py", line 15, in <module>
products_data = json.load(f)
File "C:\Users\absol\AppData\Local\Programs\Python\Python36-32\lib\json\__init__.py", line 299, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "C:\Users\absol\AppData\Local\Programs\Python\Python36-32\lib\json\__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "C:\Users\absol\AppData\Local\Programs\Python\Python36-32\lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\absol\AppData\Local\Programs\Python\Python36-32\lib\json\decoder.py", line 355, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 40 column 9 (char 1422)
如何正确导入JSON,以便在我尝试时将其发送出去?
答案 0 :(得分:0)
第39行的方括号后面有一个尾随逗号,请删除它,它应该可以正常工作
为了避免将来出现此错误,您只需在支持json格式检查的IDE中打开json即可。我使用eclipse或atom IDE,只需打开一个json文件即可显示json数据中是否存在任何语法/格式错误
删除了尾随逗号的Json:
{
"create": [
{
"name": "Woo Single #1",
"type": "simple",
"status": "draft",
"regular_price": "21.99",
"virtual": true,
"downloadable": true,
"downloads": [
{
"name": "Woo Single",
"file": "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/cd_4_angle.jpg"
}
],
"categories": [
{
"id": 11
},
{
"id": 13
}
]
},
{
"name": "New Premium Quality",
"type": "simple",
"status": "draft",
"regular_price": "21.99",
"description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.",
"short_description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.",
"categories": [
{
"id": 9
},
{
"id": 14
}
]
}
]
}
答案 1 :(得分:0)
格式错误,您添加了额外的&#34;,&#34;第39行.Javascript对象更灵活,但在使用像Python JSON这样的库进行解析时,您必须遵守RFC 4627.下次查看此网站:https://jsonformatter.curiousconcept.com
concat