如何从json数组中获取json对象

时间:2013-07-20 05:29:39

标签: json

如何从JSONArray获取JSONObject?

我有这样的JSONArray:

[
  {
    "id0": 0,
    "name0": "Test hd",
    "customerType0": "Company",
    "businessType0": "Buyer",
    "city0": "Thirunelveli"
  },
  {
    "id1": 1,
    "name1": "Abcd abcdefghij",
    "customerType1": "Company",
    "businessType1": "Buyer",
    "city1": "Varanasi"
  },
  {
    "id2": 2,
    "name2": "test ",
    "customerType2": "Company",
    "businessType2": "Buyer",
    "city2": "Erode"
  },
  {
    "id3": 3,
    "name3": "New customer",
    "customerType3": "Company",
    "businessType3": "Buyer",
    "city3": "Coimbatore"
  }
]

我想分别得到每个对象并使数据以aS Html表的格式显示。

我试过了:

function table_ajax()
{
    $.ajax({
        type: "GET",
        url: "Customergrouptable_servlet",
        data: "searchname="+$('.cnames').val(),
        success: function(data)
        {
            tableobj = JSON.parse(data)
            cusobj=tableobj.customerdetail;

            $(cusobj).each(function(index, cusobj){

                alert("index"+index);// Here i can get the index for that object. I dont know how to get the object values has stored.

            });
        }
    });
}

1 个答案:

答案 0 :(得分:0)

每次你的功能被解雇 你在cusobj上有你的阵列孩子。

现在这个样本json你有4个孩子

 [{
     "id0": 0,
     "name0": "Test hd",
     "customerType0": "Company",
     "businessType0": "Buyer",
     "city0": "Thirunelveli"
 }, {
     "id1": 1,
     "name1": "Abcd abcdefghij",
     "customerType1": "Company",
     "businessType1": "Buyer",
     "city1": "Varanasi"
 }, {
     "id2": 2,
     "name2": "test ",
     "customerType2": "Company",
     "businessType2": "Buyer",
     "city2": "Erode"
 }, {
     "id3": 3,
     "name3": "New customer",
     "customerType3": "Company",
     "businessType3": "Buyer",
     "city3": "Coimbatore"
 }]

每次你在cusobj上有一个 您可以使用

轻松访问它们

cusobj.name3,cusobj.id3

编辑: 你也走错了路 您不需要在按键后添加数字

只需使用as cusobj.name,cusobj.id