как фильтровать данные JSON с помощью JQuery - PullRequest
0 голосов
/ 08 января 2019

Это мои данные 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+"</li>";$("#setdata").html(e)}

я хочу получить displayName и phoneNumbers Пожалуйста, помогите мне, как фильтровать данные JSON

Ответы [ 2 ]

0 голосов
/ 08 января 2019

Здравствуйте, если вы хотите 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>
0 голосов
/ 08 января 2019

Вы можете использовать .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)
...