SimpleJson 处理相同命名实体
我在应用程序引擎中使用 Alchemy API,因此我使用 simplejson 库来解析响应。问题是响应中的条目具有 sme 名称
{
"status": "OK",
"usage": "By accessing AlchemyAPI or using information generated by AlchemyAPI, you are agreeing to be bound by the AlchemyAPI Terms of Use: http://www.alchemyapi.com/company/terms.html",
"url": "",
"language": "english",
"entities": [
{
"type": "Person",
"relevance": "0.33",
"count": "1",
"text": "Michael Jordan",
"disambiguated": {
"name": "Michael Jordan",
"subType": "Athlete",
"subType": "AwardWinner",
"subType": "BasketballPlayer",
"subType": "HallOfFameInductee",
"subType": "OlympicAthlete",
"subType": "SportsLeagueAwardWinner",
"subType": "FilmActor",
"subType": "TVActor",
"dbpedia": "http://dbpedia.org/resource/Michael_Jordan",
"freebase": "http://rdf.freebase.com/ns/guid.9202a8c04000641f8000000000029161",
"umbel": "http://umbel.org/umbel/ne/wikipedia/Michael_Jordan",
"opencyc": "http://sw.opencyc.org/concept/Mx4rvViVq5wpEbGdrcN5Y29ycA",
"yago": "http://mpii.de/yago/resource/Michael_Jordan"
}
}
]
}
因此问题是“subType”重复,因此加载返回的字典只是“TVActor”而不是列表。有办法解决这个问题吗?
I'm using the Alchemy API in app engine so I'm using the simplejson library to parse responses. The problem is that the responses have entries that have the sme name
{
"status": "OK",
"usage": "By accessing AlchemyAPI or using information generated by AlchemyAPI, you are agreeing to be bound by the AlchemyAPI Terms of Use: http://www.alchemyapi.com/company/terms.html",
"url": "",
"language": "english",
"entities": [
{
"type": "Person",
"relevance": "0.33",
"count": "1",
"text": "Michael Jordan",
"disambiguated": {
"name": "Michael Jordan",
"subType": "Athlete",
"subType": "AwardWinner",
"subType": "BasketballPlayer",
"subType": "HallOfFameInductee",
"subType": "OlympicAthlete",
"subType": "SportsLeagueAwardWinner",
"subType": "FilmActor",
"subType": "TVActor",
"dbpedia": "http://dbpedia.org/resource/Michael_Jordan",
"freebase": "http://rdf.freebase.com/ns/guid.9202a8c04000641f8000000000029161",
"umbel": "http://umbel.org/umbel/ne/wikipedia/Michael_Jordan",
"opencyc": "http://sw.opencyc.org/concept/Mx4rvViVq5wpEbGdrcN5Y29ycA",
"yago": "http://mpii.de/yago/resource/Michael_Jordan"
}
}
]
}
So the problem is that the "subType" is repeated so the dict that a loads returns is just "TVActor" rather than a list. Is there anyway to go around this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
定义
application/json
rfc 4627 > 说:并且:
这意味着 AlchemyAPI 不应在同一对象内返回多个
"subType"
名称并声明它是 JSON。您可以尝试以 XML 格式 (
outputMode=xml
) 请求相同内容,以避免结果出现歧义或将重复的键值转换为列表:示例
输出
The rfc 4627 that defines
application/json
says:And:
It means that AlchemyAPI should not return multiple
"subType"
names inside the same object and claim that it is a JSON.You could try to request the same in XML format (
outputMode=xml
) to avoid ambiguity in the results or to convert duplicate keys values into lists:Example
Output
application/json
媒体类型的 rfc 4627 建议使用唯一键,但不会明确禁止它们:来自 rfc 2119:
这是一个已知问题。
您可以通过修改重复的键或将其保存到数组中来解决此问题。
如果需要,您可以使用此代码。
The rfc 4627 for
application/json
media type recommends unique keys but it doesn't forbid them explicitly:From rfc 2119:
This is a known problam.
You can solve this by modify the duplicate key, or save him into array.
You can use this code if you want.