开发者

how to catch particular element of json format

开发者 https://www.devze.com 2023-03-26 05:31 出处:网络
json returns the value and I store in variable. For example开发者_Go百科 var person = { Name:\"Shree\",

json returns the value and I store in variable. For example开发者_Go百科

var person = {
    Name:"Shree",
    Address: "Ratopul",
    Profession: "Programmer",
    Address: [
        {
            District: "abc",
            Ward: "Tel",
            Tel:"235"
        },
        {
            District: "abc1",
            Ward: "Tel",
            Tel:"235"
        },
        {
            District: "abc2",
            Ward: "Tel",
            Tel: "235"
        }
    ]
};

I got a address through.

var address=person.Address;

But I want to catch a particular element of address. How is it possible. I don't have any idea. Please help.


var p1=     person.Address[0]  //first 
var p2=     person.Address[1]  //second
var p3=     person.Address[2]  //third

alert(p1.Ward);

**or**


    for (i=0; i< person.Address.length;i++)
    {
        var person=person.Address[i];
        if(person.Tel===235)
        {
           //this is the person i was looking for
        }
    }


Address is just an array of objects.

var district = person.Address[0].District;


Dude is this what u r looking for?

var addresses=person.Address;
$.each(addresses,function(index,item){
alert(item.District+"-"+item.Ward);
if(item.Tel=="235")
{
  alert(item.District);
}
});
0

精彩评论

暂无评论...
验证码 换一张
取 消