我需要帮助从URL读取JSON,其中包含以下JSON:
{
"totalItems":2,
"@href":"/classes/dsxplan:Program",
"@id":"dsxplan:Program",
"@mask":"dsplan:MVMask.WorkPackage.Complex",
"@type":"Collection",
"@code":200,
"@context":{
"dsxplan":"xplan",
"dsplan":"plan",
"dspol":"pol",
"image":{
"@id":"dspol:image",
"@type":"@id"
},
"dskern":"kern"
},
"member":[
{
"dsplan:actualType":{
"@href":"/resources/dsxplan:Program",
"@id":"dsxplan:Program",
"@mask":"dskern:Mask.Default",
"image":"iconProgram.png"
},
"dskern:owner":{
"@href":"/resources/dskern:Person.Creator",
"@id":"dskern:Person.Creator",
"@mask":"dskern:MVMask.Person.Complex",
"dsplan:actualType":{
"@href":"/resources/foaf:Person",
"@id":"foaf:Person",
"@mask":"dskern:Mask.Default"
}
},
"dspol:modificationDate":"2017-09-08T17:54:36.786Z",
"@href":"/resources/dsxplan:DSLCProgram.R-399",
"@id":"dsxplan:DSLCProgram.R-399",
"@mask":"dsplan:MVMask.WorkPackage.Complex",
"@etag":"7412df19-1dde-4245-b40b-5dd86dbbe3f1"
},
{
"dsplan:actualType":{
"@href":"/resources/dsxplan:Program",
"@id":"dsxplan:Program",
"@mask":"dskern:Mask.Default",
"image":"iconProgram.png"
},
"dskern:owner":{
"@href":"/resources/dskern:Person.Creator",
"@id":"dskern:Person.Creator",
"@mask":"dskern:MVMask.Person.Complex",
"dsplan:actualType":{
"@href":"/resources/foaf:Person",
"@id":"foaf:Person",
"@mask":"dskern:Mask.Default"
}
},
"dspol:modificationDate":"2017-09-08T17:54:36.786Z",
"@href":"/resources/dsxplan:xComModel2017program.R-394",
"@id":"dsxplan:xComModel2017program.R-394",
"@mask":"dsplan:MVMask.WorkPackage.Complex",
"@etag":"7412df19-1dde-4245-b40b-5dd86dbbe3f1"
}
]
}
我只需要从提供的链接中读取这个json。我尝试了以下代码:
import urllib.request
request= urllib.request.Request("https://dummy_link")
response = urllib.request.urlopen(request)
input = (response.read().decode('utf-8'))
json.loads(input)
此代码抛出此错误: " JSONDecodeError:期望值:第9行第1列(字符12)"
你能帮我解决这个问题吗?我真的很感激帮助。!!
答案 0 :(得分:0)
您可以使用比urllib更简单的Requests库:
例如:
import requests
r = requests.get('https://dummy_link')
obj = r.json()
修改强>
如果您想使用urllib,可以执行以下操作:
import urllib.request
import json
with urllib.request.urlopen("https://dummy_link") as f:
content = f.read()
obj = json.loads(content)
无需将二进制内容转换为unicode字符串。
官方文档中有urllib howto。