所以我有下一个json,我只想得到github链接和twitter链接,而不需要foursquare和gravatar链接。你知道吗
当然,有时候json数据会改变,如果找不到foursquare的url,github会成为第一个,twitter会成为第二个。其他的(github或twitter)也一样。你知道吗
如果github和twitter的url与json中的位置不同,如何获取它们?你知道吗
{ "socialProfiles": [
{
"type": "foursquare",
"typeId": "foursquare",
"typeName": "Foursquare",
"url": "https://foursquare.com/user/somerandomuser",
"id": "554225246246"
},
{
"type": "github",
"typeId": "github",
"typeName": "Github",
"url": "https://github.com/somerandomuser",
"username": "somerandomuser"
},
{
"type": "gravatar",
"typeId": "gravatar",
"typeName": "Gravatar",
"url": "https://gravatar.com/somerandomuser",
"username": "somerandomuser",
"id": "132341667"
},
{
"bio": " This is a bio of a random user",
"followers": 543,
"following": 222,
"type": "twitter",
"typeId": "twitter",
"typeName": "Twitter",
"url": "https://twitter.com/somerandomuser",
"username": "somerandomuser",
"id": "41414515335"
}
]
}
使用简单的迭代。你知道吗
例如:
输出:
data
是你的字典。你知道吗您可以使用
dict comprehension
来创建另一个dict
,只使用您需要的url,在本例中是twitter
和github
输出
然后你就可以得到你需要的
url
。你知道吗相关问题 更多 >
编程相关推荐