2013-07-20 200 views
-1

如何從JSONArray中獲取JSONObject?如何從json數組中獲取json對象

我有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" 
    } 
] 

,我希望得到每個對象seperately並提出,要在格式爲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. 

      }); 
     } 
    }); 
} 
+0

請縮進您的代碼,以便閱讀。它只需要一分鐘,它確實有幫助。爲了使格式化JSON變得更容易,試試這個:'JSON.stringify(obj,null,'')' – tjameson

回答

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 其中之一,你可以使用easyli

cusobj.name3他們訪問時間,cusobj.id3

編輯: 也你錯了路 你不需要在鍵後添加數字

只用作 cusobj.name,cusobj.id