最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Parsing JSON array with JQuery - Stack Overflow

programmeradmin1浏览0评论

I am having the most difficult time parsing the json below using jquery.

{ "client": [{"ClientID": "0000000001", "Name": "Valdez Gilberto JR",   "Phone": "(956)542-8148" , "MedActID": "10", "Gender": "M", "Division": "WP", "Address": "1905 Illinois Ave N", "Class": "", "CityState": "Brownsville TX 78521-6732" } , {"ClientID": "0000000002", "Name": "Salazar Olga F", "Phone": "(956)546-3909" , "MedActID": "100", "Gender": "F", "Division": "MP", "Address": "Route 8 Box 626 (San Pedro)", "Class": "", "CityState": "Brownsville TX 78520" } ]}

Been using the code below but to no avail, keep getting undefined errors "data" is being called via ajax:

var obj = JSON.parse(data);
for(var i = 0; i < obj.length; i++){
alert(obj[i].client.Name)
}

Have even tried the following:

$.each(obj, function(key,value) {
 alert(value.client.Name);
});

I am having the most difficult time parsing the json below using jquery.

{ "client": [{"ClientID": "0000000001", "Name": "Valdez Gilberto JR",   "Phone": "(956)542-8148" , "MedActID": "10", "Gender": "M", "Division": "WP", "Address": "1905 Illinois Ave N", "Class": "", "CityState": "Brownsville TX 78521-6732" } , {"ClientID": "0000000002", "Name": "Salazar Olga F", "Phone": "(956)546-3909" , "MedActID": "100", "Gender": "F", "Division": "MP", "Address": "Route 8 Box 626 (San Pedro)", "Class": "", "CityState": "Brownsville TX 78520" } ]}

Been using the code below but to no avail, keep getting undefined errors "data" is being called via ajax:

var obj = JSON.parse(data);
for(var i = 0; i < obj.length; i++){
alert(obj[i].client.Name)
}

Have even tried the following:

$.each(obj, function(key,value) {
 alert(value.client.Name);
});
Share Improve this question edited Jul 1, 2019 at 7:29 Arnaud 7,45910 gold badges52 silver badges72 bronze badges asked Aug 8, 2016 at 19:46 DangoDango 1592 silver badges15 bronze badges 4
  • 2 to be clear.. you're not having any trouble at all "parsing" the json, you're having trouble looping over the resulting object. For some reason you're treating the object as if it were an array. – Kevin B Commented Aug 8, 2016 at 19:48
  • How would I correct the issue? Driving me nuts... Have even tried – Dango Commented Aug 8, 2016 at 19:52
  • right... but what makes you think obj is an array or has a length? clearly your json represents an object that has a property that contains an array. – Kevin B Commented Aug 8, 2016 at 19:53
  • @Dango did it solve ur issue? – Iceman Commented Aug 13, 2016 at 18:37
Add a ment  | 

3 Answers 3

Reset to default 3

You should grab the "client" property first which is an array. Once you grab it, then you can iterate over it as an array.

{                                        //object              | obj
   "client":[                            //property (array)    | obj.client
      {                                  //object inside array | obj.client[0]
         "ClientID":"0000000001",
         "Name":"Valdez Gilberto JR",    //property            | obj.client[0].Name
         "Phone":"(956)542-8148",
         "MedActID":"10",
         "Gender":"M",
         "Division":"WP",
         "Address":"1905 Illinois Ave N",
         "Class":"",
         "CityState":"Brownsville TX 78521-6732"
      },
      {
         "ClientID":"0000000002",
         "Name":"Salazar Olga F",
         "Phone":"(956)546-3909",
         "MedActID":"100",
         "Gender":"F",
         "Division":"MP",
         "Address":"Route 8 Box 626 (San Pedro)",
         "Class":"",
         "CityState":"Brownsville TX 78520"
      }
   ]
}

This code will iterate over the objects inside the "client" property, and alert the names.

var obj = JSON.parse(data);
var client = obj.client; //client prop is an array
for(var i = 0; i < client.length; i++){
  alert(client[i].Name);
}

You are trying to iterate an object, but you can't do that. What you want is to iterate trough the array which is in your object, keyed by client

Try this:

for(var i = 0; i < obj.client.length; i++){
    console.log(obj.client[i].Name)
}

You are trying to iterate through an object. obj.client is the Array you are looking for.

var data = '{ "client": [{"ClientID": "0000000001", "Name": "Valdez Gilberto JR",   "Phone": "(956)542-8148" , "MedActID": "10", "Gender": "M", "Division": "WP", "Address": "1905 Illinois Ave N", "Class": "", "CityState": "Brownsville TX 78521-6732" } , {"ClientID": "0000000002", "Name": "Salazar Olga F", "Phone": "(956)546-3909" , "MedActID": "100", "Gender": "F", "Division": "MP", "Address": "Route 8 Box 626 (San Pedro)", "Class": "", "CityState": "Brownsville TX 78520" } ]}';
var obj = JSON.parse(data);
for (var i = 0; i < obj.client.length; i++) {
  console.log(obj.client[i].Name);
}

发布评论

评论列表(0)

  1. 暂无评论