所以我有下一个json,我只想获取github链接和twitter,而不必获取foursquare和gravatar一个。
这还不是全部,有时json数据可以更改,如果找不到Foursquare网址,则github将成为第一个,而twitter将成为第二个。与其他人(github或twitter)相同。
如果github和twitter网址与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"
}
]
}
答案 0 :(得分:0)
使用简单的迭代。
例如:
int main() {
cpu_set_t mask;
int temp[FILENAME_MAX]
for(int i = 0; i < CPU_SETSIZE; i++)
{
if(CPU_ISSET(i, &mask))
{
temp[i] = i;
}
}
for(int i: temp)
os << i;
string str(os.str());
cout << str;
return 0;
}
输出:
checkList = ["twitter", "github"]
for i in data["socialProfiles"]:
if i["typeId"] in checkList: #Check if typeid is in your check-list
print(i["url"])
https://github.com/somerandomuser
https://twitter.com/somerandomuser
是您的字典。答案 1 :(得分:0)
您只能使用dict comprehension
和dict
的网址来使用twitter
创建另一个github
search = ['github', 'twitter']
urls = {dct['type']: dct['url'] for dct in data.get('socialProfiles', []) if dct['type'] in search}
print(urls)
输出
{
'github': 'https://github.com/somerandomuser',
'twitter': 'https://twitter.com/somerandomuser'
}
然后,您将获得所需的url
。
print(urls['github'])
# Output
# https://github.com/somerandomuser
答案 2 :(得分:0)
ES6: let newArr = arr.filter(e => e);