0

I am trying to learn how to retrieve data from a json obj that I return from PHP but I can not figure out how to get the values. My data looks something like this:

[{"user_name":"herp"},{"email":"[email protected]"},{"yy":"yyyy"},{"mm":"mm"},{"dd":"dd"}]

My client-side script looks something like this:

        $.ajax({
        type : 'POST',
        url : 'serverside/get_installningar.php',
        dataType : 'json',
        success : function(data) {

        }
    });

I would like to type something like data.user_name to retrieve the user_name and so on. But is there a method for doing this? I have looked in the forum but can't find the right thing.

5 Answers 5

5

What you have is an array of objects and so you going to need to know where it is to get user_name,

data[0].user_name

see below for structural details,

[
 {"user_name":"herp"},   // <-- data[0]
 {"email":"[email protected]"}, // <-- data[1]
 {"yy":"yyyy"},  // <-- data[2]
 {"mm":"mm"},    // <-- data[3]
 {"dd":"dd"}     // <-- data[4]
]

As AndrewR pointed out,

This will work, but it would be better to fix the JSON format coming from PHP. {"user_name":"herp","email":"[email protected]","yy":"yyyy","mm":"mm","dd":"d‌​d"} and then his original plan will work.

Sign up to request clarification or add additional context in comments.

1 Comment

This will work, but it would be better to fix the JSON format coming from PHP. {"user_name":"herp","email":"[email protected]","yy":"yyyy","mm":"mm","dd":"dd"} and then his original plan will work.
0

as of jquery 1.4.1 you can do this natively

jQuery.parseJSON

Do that on the data.responseJSON to convert to JS object.

See How do I convert a JSON string to a JavaScript object in jQuery?

Comments

0

Use data.parseJSON();. It returns an object exactly the way you want it.

1 Comment

No. jQuery already does it for you, since the dataType is json which will parse the JSON before returning it to you.
0

You are actually returning an array not a json object

Your data needs to look like this:

{{"user_name":"herp"},{"email":"[email protected]"},{"yy":"yyyy"},{"mm":"mm"},{"dd":"dd"}}

Try installing Firebug to debug the return value of your page if its a properly formatted JSON.

Comments

0

use it like:

 console.log(data[0].user_name);

For a easy acces members of your json, try virtualising it in a nice format so you can understand where you have array and where you have object.

[] means array

{} means object

I recommand a chrome extension: JsonView

and for example, take a JSON request like this.

Just open it in a tab and it will be nicely formated. Also it shows you in the bottom left corner how to access what you are hovering over.

2 Comments

No; jQuery does that for you.
dataType : 'json' in the ajax call was enough. But console.log(data[0].user_name); was what I was looking for! Cheers!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.