简体   繁体   中英

How to retrieve values from json

I'm new to json , so plz do help me on this. I'm trying to retrieve value from this json eg:I want to get fname from this json,. How do I get this value ; I tried showdata.fname , showdata.id[0].fname, but not getting I know this is stupid quest to ask but plz help me.

var showdata = {
    "fc1d3f54-bcd3-2c4d-2626-cb9904e63800": {
        "fname": "Nitish",
        "lname": "pakhare",
        "phoneno": "4545445",
        "id": "fc1d3f54-bcd3-2c4d-2626-cb9904e63800"
    },
    "6ae08ee6-b02d-0eeb-4ead-1d52bbdadf5e": {
        "fname": "Ashish",
        "lname": "Pakahre",
        "phoneno": "454545",
        "id": "6ae08ee6-b02d-0eeb-4ead-1d52bbdadf5e"
    },
    "7e418c15-17c1-da8e-b614-b362f7937eb9": {
        "fname": "Arpita",
        "lname": "kimar",
        "phoneno": "454545",
        "id": "7e418c15-17c1-da8e-b614-b362f7937eb9"
    }
}

This code logs all fname properties inside this object.

for( var key in showdata ) {
  if ( showdata.hasOwnProperty( key ) ) {
     console.log( showdata[ key ][ 'fname' ] );
  }
}

You actually have an object with multiple properties. The above code traverses all properties, checks whether the property is actually present in this object (this solves some issues coming from the prototypical nature of JavaScript objects) and the logs the values. You should replace the logging with your program logic.

Easier would be an implementation using an array instead of an object. That way you could use all the native JavaScript array functions like map etc. In that case, however, you would loose your keys (unless you add them as an additional property in the sub-objects). This would look like the following:

var showdata = [
    {
        "key": "fc1d3f54-bcd3-2c4d-2626-cb9904e63800",
        "fname": "Nitish",
        "lname": "pakhare",
        "phoneno": "4545445",
        "id": "fc1d3f54-bcd3-2c4d-2626-cb9904e63800"
    },
    {
        "key": "6ae08ee6-b02d-0eeb-4ead-1d52bbdadf5e",
        "fname": "Ashish",
        "lname": "Pakahre",
        "phoneno": "454545",
        "id": "6ae08ee6-b02d-0eeb-4ead-1d52bbdadf5e"
    },
    {
        "key": "7e418c15-17c1-da8e-b614-b362f7937eb9",
        "fname": "Arpita",
        "lname": "kimar",
        "phoneno": "454545",
        "id": "7e418c15-17c1-da8e-b614-b362f7937eb9"
    }
];

showdata.fc1d3f54-bcd3-2c4d-2626-cb9904e63800.fname应该是答案,但我不确定它是否可以工作。

showdata["fc1d3f54-bcd3-2c4d-2626-cb9904e63800"].fname

Actually, this is the correct syntax.

See this tinker: JSON Tinker

If you want to access them, you can do:

showdata.fc1d3f54-bcd3-2c4d-2626-cb9904e63800.fname

but that would be hardcoding it. Instead, you can access them via loop.

var key, entry;

for(key in showdata){
    if(showdata.hasOwnProperty(key)){
        entry = showdata[key];
        //entry.fname
        //entry.lname
        //entry.phoneno
        //entry.id, which would be the same as key in your case
    }
}

you can also consider using an array, so that you won't hardcode keys:

var showdata = [
    {
        "fname": "Nitish",
        "lname": "pakhare",
        "phoneno": "4545445",
        "id": "fc1d3f54-bcd3-2c4d-2626-cb9904e63800"
    }, {
        "fname": "Ashish",
        "lname": "Pakahre",
        "phoneno": "454545",
        "id": "6ae08ee6-b02d-0eeb-4ead-1d52bbdadf5e"
    }, {
        "fname": "Arpita",
        "lname": "kimar",
        "phoneno": "454545",
        "id": "7e418c15-17c1-da8e-b614-b362f7937eb9"
    }
]

//showdata[0].fname

Looks like you're using the long HEX strings as keys, you would access an item like

showdata['fc1d3f54-bcd3-2c4d-2626-cb9904e63800']['fname']

The square bracket notation is preferred to dot notation, as it is more robust for looking things up, especially with long keys like that.

showdata['fc1d3f54-bcd3-2c4d-2626-cb9904e63800'].fname 

would be ok too

if you want it to be indexed with integers, perhaps you could structure it like:

var showdata = [
    { 
    "fname": "Nitish",
    "lname": "pakhare",
    "phoneno": "4545445",
    "id": "fc1d3f54-bcd3-2c4d-2626-cb9904e63800"
    },
    {
    "fname": "Ashish",
    "lname": "Pakahre",
    "phoneno": "454545",
    "id": "6ae08ee6-b02d-0eeb-4ead-1d52bbdadf5e"
    }
]

As an array of objects, then you can do

showdata[0].fname

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM