How to access JSON Object name/value?

JqueryAjaxJsonasp.net Mvc-3

Jquery Problem Overview


function (data) {
    //add values based on activity type
    //data = JSON.parse(data);
    //alert(abc.Phone1);
             
    alert(data.myName)
            
    alert(data.toString());
    if (activityType == "Phone") {
    }
    return;

},

As you can see this callback function of $.ajax taking JSON data from controller.

For example:

[{"name":"myName" ,"address": "myAddress" }]

In this case my first alert giving me undefined and second/third alert popup comes up with:

[{"name":"myName" ,"address": "myAddress" }]

How can I access value by name so that my first alert filled out with myName which is value of name?

Jquery Solutions


Solution 1 - Jquery

In stead of parsing JSON you can do like followng:

$.ajax({
  ..
  dataType: 'json' // using json, jquery will make parse for  you
});

To access a property of your JSON do following:

data[0].name;

data[0].address;

Why you need data[0] because data is an array, so to its content retrieve you need data[0] (first element), which gives you an object {"name":"myName" ,"address": "myAddress" }.

And to access property of an object rule is:

Object.property

or sometimes

Object["property"] // in some case

So you need

data[0].name and so on to get what you want.


If you not

set dataType: json then you need to parse them using $.parseJSON() and to retrieve data like above.

Solution 2 - Jquery

The JSON you are receiving is in string. You have to convert it into JSON object You have commented the most important line of code

data = JSON.parse(data);

Or if you are using jQuery

data = $.parseJSON(data)

Solution 3 - Jquery

If you response is like {'customer':{'first_name':'John','last_name':'Cena'}}

var d = JSON.parse(response);
alert(d.customer.first_name); // contains "John"

Thanks,

Solution 4 - Jquery

You should do

alert(data[0].name); //Take the property name of the first array

and not

 alert(data.myName)

jQuery should be able to sniff the dataType for you even if you don't set it so no need for JSON.parse.

fiddle here

http://jsfiddle.net/H2yN6/

Solution 5 - Jquery

Try this code..

function (data) {


var json = jQuery.parseJSON(data);
alert( json.name );


}

Solution 6 - Jquery

You might want to try this approach:

var  str ="{ "name" : "user"}";
var jsonData = JSON.parse(str);		
console.log(jsonData.name)
//Array Object
str ="[{ "name" : "user"},{ "name" : "user2"}]";
jsonData = JSON.parse(str);		
console.log(jsonData[0].name)

Solution 7 - Jquery

I think you should mention dataType: 'json' in ajax config and to access that value:

data[0].name

Solution 8 - Jquery

Here is a friendly piece of advice. Use something like Chrome Developer Tools or Firebug for Firefox to inspect your Ajax calls and results.

You may also want to invest some time in understanding a helper library like Underscore, which complements jQuery and gives you 60+ useful functions for manipulating data objects with JavaScript.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRollerCostaView Question on Stackoverflow
Solution 1 - JquerythecodeparadoxView Answer on Stackoverflow
Solution 2 - JqueryU.PView Answer on Stackoverflow
Solution 3 - JqueryMahendran SakkaraiView Answer on Stackoverflow
Solution 4 - JqueryNicola PeluchettiView Answer on Stackoverflow
Solution 5 - JqueryKabilan SView Answer on Stackoverflow
Solution 6 - JqueryMansuri NurulhudaView Answer on Stackoverflow
Solution 7 - JqueryThe System RestartView Answer on Stackoverflow
Solution 8 - JqueryButifarraView Answer on Stackoverflow