2013-07-20 4 views
-1

Comment obtenir JSONObject de JSONArray?comment obtenir l'objet json de json array

j'avais JSONArray comme ceci:

[ 
    { 
    "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" 
    } 
] 

Et je veux obtenir chaque objet et fait que séparement données à afficher dans le format tableau Html.

J'ai essayé:

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

S'il vous plaît indenter votre code afin qu'il soit facile à lire. Cela ne prend qu'une minute et ça aide vraiment. Pour faciliter la mise en forme de JSON, essayez ceci: 'JSON.stringify (obj, null, '')' – tjameson

Répondre

0

chaque fois que votre fonction vous avez tiré votre enfant de tableau sur cusobj.

maintenant sur cet échantillon JSON vous avez 4 enfants

[{ 
    "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" 
}] 

et chaque fois que vous avez un d'entre eux sur cusobj vous pouvez les utiliser easyli l'accès

cusobj.name3, cusobj.id3

EDIT: aussi, vous êtes sur le mauvais sens vous n'avez pas besoin d'ajouter nombre après clés

juste utiliser comme cusobj.name, cusobj.id