Reputation: 57
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"
}
}
Upvotes: 0
Views: 244
Reputation: 74036
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"
}
];
Upvotes: 1
Reputation: 305
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
Upvotes: 0
Reputation: 119827
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
Upvotes: 0
Reputation: 47172
showdata["fc1d3f54-bcd3-2c4d-2626-cb9904e63800"].fname
Actually, this is the correct syntax.
See this tinker: JSON Tinker
Upvotes: 0
Reputation: 9034
showdata.fc1d3f54-bcd3-2c4d-2626-cb9904e63800.fname
should be the answer but I'm not sure it will work.
Upvotes: 0