0

I've been plowing this website for a long, long time but can't find the answer to how to convert this json array into human readable and usable values.

An alert shows decently this:

{"item1":"value1","item2":"value2","item3":"value3"}

I want to use the values like this:

succes: function(data) {
    alert(value1);
    $("#div").append(value1);
}

But it seems I just don't have this in my Jquery vocabulaire yet. All alerts give undefined values.

Could sure use some help here!

Edit:

$.ajax({ 
        type: "POST", 
        url: "/include/process/xxxx.php", 
        data: dataString, 
        success: function(data) { 
           alert(data); alert(data.item1); 
        } 
});
2
  • 1
    If the JSON is being retured properly back from the server, it should be inside the response object on success. Can you please show the full javascript ajax call to start? Commented Aug 3, 2012 at 22:35
  • $.ajax({ type: "POST", url: "/include/process/xxxx.php", data: dataString, success: function(data) { alert(data); alert(data.item1); } }); Commented Aug 3, 2012 at 22:50

4 Answers 4

2
success: function(data) {
    alert(data.item1); // alerts "value1"
    $("#div").append(data.item1); // appends "value1"
}

item1, and similarly item2, etc. are the keys. To get a value associated with a key, you must reference the key itself, not the value (i.e., value1)

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

2 Comments

Anonymous down-voter, please let me know why so I can improve the answer if need be.
Seriously, sometimes Stackoverflow feels like Reddit.
2

If alert(value1); is showing {"item1":"value1","item2":"value2","item3":"value3"} then it is not an json object (otherwise the alert would show something like [object Object]). If you are using $.ajax ensure you are setting dataType: "json" correctly, otherwise try doing this on the data first.

var obj = $.parseJSON(data);

1 Comment

Prowla, you did it! The alerts show the right values now. Funny enough, first I got errors on using dataType: 'json'. Added it, and all seems fine now. And the alert gives 'object' instead of the full array in the alert box. Needless to say you guys all rock!
0

Try:

alert(data.item1);

You are currently doing an alert with value1, which isn't anything at that point in your code, so when that alert pops up, you aren't seeing what you expected.

5 Comments

Thanks everyone, and sorry, because "alert(data.item1);" keeps returning undefined. I am lost since the array alert seems to be right.
What browser are you using? In Chrome, you can set a break point on the first line of the success callback and look at the data object. Or just add console.log(data); in the callback, and look in the console to see what data came back. On a Windows machine, Control - Shift - J will open the console in Chrome. See what the data looks like, maybe copy/paste the results into your question. There's bound to be something in it that will help us figure this out!
Using IE9 but tested in FF just now. Values keep returning undefined.
It would be great to see the full request and response. I love the Network tab in Developer Tools in Chrome. Firebug allows you to see this information in Firefox. IE also has a Network tab in its developer tools (F12 to open). You can then see the data sent to the server, and received from it. Fiddler would work as well.
Yeah, sorry for that, Prowla just opted the solution. Now the array shows as Object in the alert box. Now the data values get processed. Super thanks for your cooperation. Gromer. This site really rocks!
0

There is a typo in your code ,succes should be success, you can read the properties of the object this way:

success: function(data) {
    alert(data.item1);
    $("#div").append(data.item1);
}

2 Comments

ps. the typo was only my copy/paste error. I type the questions in textpad first.
@KjS okay, try this alert(data[0]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.