-1

如何从 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.

            });
        }
    });
}
4

1 回答 1

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

编辑:你也是错误的方式,你不需要在键后添加数字

只需用作 cusobj.name , cusobj.id

于 2013-07-20T05:35:37.120 回答