这是我的JSON数据:
[{"id":"1","rawId":"1","displayName":"user name 1","name":{"familyName":"user","givenName":"name","formatted":"user name 1"},"nickname":null,"phoneNumbers":[{"id":"2","pref":false,"value":"9874563210","type":"other"}],"emails":null,"addresses":null,"ims":null,"organizations":null,"birthday":null,"note":null,"photos":null,"categories":null,"urls":"http://www.user1.com"},{"id":"2","rawId":"1","displayName":"user name 2","name":{"familyName":"user","givenName":"name","formatted":"user name 2"},"nickname":null,"phoneNumbers":[{"id":"2","pref":false,"value":"9632587410","type":"other"}],"emails":null,"addresses":null,"ims":null,"organizations":null,"birthday":null,"note":null,"photos":null,"categories":null,"urls":"http://www.yahoo.com"},{"id":"3","rawId":"1","displayName":"user name 3","name":{"familyName":"user","givenName":"name","formatted":"user name 3"},"nickname":null,"phoneNumbers":[{"id":"2","pref":false,"value":"9858745852","type":"other"}],"emails":null,"addresses":null,"ims":null,"organizations":null,"birthday":null,"note":null,"photos":null,"categories":null,"urls":null},{"id":"4","rawId":"1","displayName":"Distress Number","name":{"familyName":"Number","givenName":"Distress","formatted":"Distress Number"},"nickname":null,"phoneNumbers":[{"id":"2","pref":false,"value":"8547125693","type":"other"}],"emails":null,"addresses":null,"ims":null,"organizations":null,"birthday":null,"note":null,"photos":null,"categories":null,"urls":"http://www.goo.com"}]
javascript
function onSuccess(a){for(var e="",l=0;l"+a[l].displayName+":"+a[l].phoneNumber[0].value+"";$("#setdata").html(e)}
我想获取displayName和phoneNumbers 请引导我如何过滤json数据
答案 0 :(得分:0)
您可以使用.map()创建仅包含相关内容的对象新数组。
以下内容创建了一个新的对象数组-仅具有每个用户的displayName和电话号码数组。
var data = [{"id":"1","rawId":"1","displayName":"user name 1","name":{"familyName":"user","givenName":"name","formatted":"user name 1"},"nickname":null,"phoneNumbers":[{"id":"2","pref":false,"value":"9874563210","type":"other"}],"emails":null,"addresses":null,"ims":null,"organizations":null,"birthday":null,"note":null,"photos":null,"categories":null,"urls":"http://www.user1.com"},{"id":"2","rawId":"1","displayName":"user name 2","name":{"familyName":"user","givenName":"name","formatted":"user name 2"},"nickname":null,"phoneNumbers":[{"id":"2","pref":false,"value":"9632587410","type":"other"}],"emails":null,"addresses":null,"ims":null,"organizations":null,"birthday":null,"note":null,"photos":null,"categories":null,"urls":"http://www.yahoo.com"},{"id":"3","rawId":"1","displayName":"user name 3","name":{"familyName":"user","givenName":"name","formatted":"user name 3"},"nickname":null,"phoneNumbers":[{"id":"2","pref":false,"value":"9858745852","type":"other"}],"emails":null,"addresses":null,"ims":null,"organizations":null,"birthday":null,"note":null,"photos":null,"categories":null,"urls":null},{"id":"4","rawId":"1","displayName":"Distress Number","name":{"familyName":"Number","givenName":"Distress","formatted":"Distress Number"},"nickname":null,"phoneNumbers":[{"id":"2","pref":false,"value":"8547125693","type":"other"}],"emails":null,"addresses":null,"ims":null,"organizations":null,"birthday":null,"note":null,"photos":null,"categories":null,"urls":"http://www.goo.com"}]
let details = data.map(item => { return {displayName: item.displayName, phoneNumbers: item.phoneNumbers}})
console.log(details)
答案 1 :(得分:0)
您好,如果您想使用jquery,则可以使用每个(http://api.jquery.com/jquery.each/),然后附加一个工作示例,该示例以div显示名称+第一个电话号码显示,如果要显示所有电话号码,则可以添加另一个电话号码,让我知道您是否需要帮助。
let data = [{"id":"1","rawId":"1","displayName":"user name 1","name":{"familyName":"user","givenName":"name","formatted":"user name 1"},"nickname":null,"phoneNumbers":[{"id":"2","pref":false,"value":"9874563210","type":"other"}],"emails":null,"addresses":null,"ims":null,"organizations":null,"birthday":null,"note":null,"photos":null,"categories":null,"urls":"http://www.user1.com"},{"id":"2","rawId":"1","displayName":"user name 2","name":{"familyName":"user","givenName":"name","formatted":"user name 2"},"nickname":null,"phoneNumbers":[{"id":"2","pref":false,"value":"9632587410","type":"other"}],"emails":null,"addresses":null,"ims":null,"organizations":null,"birthday":null,"note":null,"photos":null,"categories":null,"urls":"http://www.yahoo.com"},{"id":"3","rawId":"1","displayName":"user name 3","name":{"familyName":"user","givenName":"name","formatted":"user name 3"},"nickname":null,"phoneNumbers":[{"id":"2","pref":false,"value":"9858745852","type":"other"}],"emails":null,"addresses":null,"ims":null,"organizations":null,"birthday":null,"note":null,"photos":null,"categories":null,"urls":null},{"id":"4","rawId":"1","displayName":"Distress Number","name":{"familyName":"Number","givenName":"Distress","formatted":"Distress Number"},"nickname":null,"phoneNumbers":[{"id":"2","pref":false,"value":"8547125693","type":"other"}],"emails":null,"addresses":null,"ims":null,"organizations":null,"birthday":null,"note":null,"photos":null,"categories":null,"urls":"http://www.goo.com"}];
let content = "";
$.each(data, function(index, value){
content += value.displayName+" - "+value.phoneNumbers[0].value+"<br>";
});
$('#results').html(content);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="results">
</div>