[英]JSON serialization Error in Python 3.2
I am using JSON library and trying to import a page feed to an CSV file. 我正在使用JSON库,并尝试将页面供稿导入CSV文件。 Tried many a ways to get the result however every time code execute it Gives JSON not serialzable.
尝试了多种方法来获取结果,但是每次执行代码时都会使JSON无法序列化。 No Facebook use auth code which I have and used it so connection string will change however if you use a page which has public privacy you will still be able to get the result from below code.
没有Facebook使用我已经使用过的身份验证代码,因此连接字符串将发生变化,但是,如果您使用的页面具有公共隐私,您仍然可以从下面的代码中获取结果。
following is the code 以下是代码
import urllib3
import json
import requests
#from pprint import pprint
import csv
from urllib.request import urlopen
page_id = "abcd" # username or id
api_endpoint = "https://graph.facebook.com"
fb_graph_url = api_endpoint+"/"+page_id
try:
#api_request = urllib3.Requests(fb_graph_url)
#http = urllib3.PoolManager()
#api_response = http.request('GET', fb_graph_url)
api_response = requests.get(fb_graph_url)
try:
#print (list.sort(json.loads(api_response.read())))
obj = open('data', 'w')
# write(json_dat)
f = api_response.content
obj.write(json.dumps(f))
obj.close()
except Exception as ee:
print(ee)
except Exception as e:
print( e)
Tried many approach but not successful. 尝试了许多方法,但未成功。 hope some one can help
希望有人可以帮忙
api_response.content
is the text content of the API, not a Python object so you won't be able to dump it. api_response.content
是API的文本内容,而不是Python对象,因此您将无法转储它。
Try either: 尝试以下任一方法:
f = api_response.content
obj.write(f)
Or 要么
f = api_response.json()
obj.write(json.dumps(f))
requests.get(fb_graph_url).content
is probably a string. 可能是一个字符串。 Using
json.dumps
on it won't work. 在上面使用
json.dumps
将不起作用。 This function expects a list or a dictionary as the argument. 此函数需要列表或字典作为参数。
If the request already returns JSON, just write it to the file. 如果请求已返回JSON,则只需将其写入文件即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.