我如何编辑这个无效的 json 并将其加载到 python
How do i edit this invalid json and parse load it in python
jsonCallback1530150433250_46028 && jsonCallback1530150433250_46028({"context":"synthesis%3Ddisabled%26q%3D%2523all%2Bcard_content_lang %253Aen%2B%2B%2B%2Bcard_content_type%253A%2528%2522career%2522%2529%2B%26b%3D0%26s%3Ddesc%2528score_with_card_update_timestamp%2529%26output_format%3D json%26回调%3
以上是 json 的部分示例,我想从下面的完整 link 中解析出来。
主要问题是我无法从网上提取它,因为它无效。我以前使用 json.loads() 但结构发生了变化。我如何将整个 json 数据用不同的库或其他任何东西放入字符串中进行编辑?
感谢帮助
我现在不能post一张图片,但这是一个类比:
link = 'https://apisearch.3ds.com/card_search_api?q=%23all%20card_content_lang%3Aen%20%20%20%20card_content_type%3A(%22career%22)%20&s=desc(score_with_card_update_timestamp)&b=0&hf=10&output_format=json&callback=jsonCallback1530150433250_46028'
response = requests.get(link)
time.sleep(random.randint(3,5)
json_obj = json.loads(response.text)
print json_obj
这给了我,*ValueError: No JSON object could be decoded*
我不会在此处复制您的整个 JSON 数据,但这是要点。
x = "jsonCallback1530150433250_46028 && jsonCallback1530150433250_46028({the_json_data_you_want}"
我要在第一个“(
”处拆分,因为您想要后面的所有内容:
a = x.split('(', 1)
reqJson = a[1]
print(reqJson)
这将为您提供所需的 JSON。现在您可以:
import json
json_data = json.loads(reqJson)
然后继续你想要达到的目标。
PS:请以可读的格式编辑您的问题,以便其他人在您需要进一步帮助时也能帮助您。
您可能需要做的是截断匹配的字符串,直到找到“{”
为此:
# split the json string with '{' as separator:
va1='jhdgfhsgdf&&sdkfhskjdfh({sdfhsj({})dfhsj})'
v1=va1.split('{')
# now delete the first element of the array
del v1[0]
# now join rest of the elements of the array to get proper json string
'{'.join(v1)
jsonCallback1530150433250_46028 && jsonCallback1530150433250_46028({"context":"synthesis%3Ddisabled%26q%3D%2523all%2Bcard_content_lang %253Aen%2B%2B%2B%2Bcard_content_type%253A%2528%2522career%2522%2529%2B%26b%3D0%26s%3Ddesc%2528score_with_card_update_timestamp%2529%26output_format%3D json%26回调%3
以上是 json 的部分示例,我想从下面的完整 link 中解析出来。
主要问题是我无法从网上提取它,因为它无效。我以前使用 json.loads() 但结构发生了变化。我如何将整个 json 数据用不同的库或其他任何东西放入字符串中进行编辑?
感谢帮助
我现在不能post一张图片,但这是一个类比:
link = 'https://apisearch.3ds.com/card_search_api?q=%23all%20card_content_lang%3Aen%20%20%20%20card_content_type%3A(%22career%22)%20&s=desc(score_with_card_update_timestamp)&b=0&hf=10&output_format=json&callback=jsonCallback1530150433250_46028'
response = requests.get(link)
time.sleep(random.randint(3,5)
json_obj = json.loads(response.text)
print json_obj
这给了我,*ValueError: No JSON object could be decoded*
我不会在此处复制您的整个 JSON 数据,但这是要点。
x = "jsonCallback1530150433250_46028 && jsonCallback1530150433250_46028({the_json_data_you_want}"
我要在第一个“(
”处拆分,因为您想要后面的所有内容:
a = x.split('(', 1)
reqJson = a[1]
print(reqJson)
这将为您提供所需的 JSON。现在您可以:
import json
json_data = json.loads(reqJson)
然后继续你想要达到的目标。
PS:请以可读的格式编辑您的问题,以便其他人在您需要进一步帮助时也能帮助您。
您可能需要做的是截断匹配的字符串,直到找到“{” 为此:
# split the json string with '{' as separator:
va1='jhdgfhsgdf&&sdkfhskjdfh({sdfhsj({})dfhsj})'
v1=va1.split('{')
# now delete the first element of the array
del v1[0]
# now join rest of the elements of the array to get proper json string
'{'.join(v1)