2

I have a set of result which i have converted to Json using php json_encode function now i want this result as an array in js but I am not getting an idea to do this.

My PHP array is like this :

Array
(
    [latestRating] => Array
        (
            [456] => Anonymous has rated xxx9
            [457] => Anonymous has rated xxxx8
            [458] => Anonymous has rated xxxx3.5
        )

    [latestUser] => Array
        (
            [242] => xxxxhas just signed up
            [243] => xxxxxhas just signed up
            [244] => xxxxxxhas just signed up
        )

)

When i do a php json_encode function on this i get following string

{"latestRating":{"456":"Anonymous has rated mermaidbl00d 9","457":"Anonymous has rated GeorgiaHallx 8","458":"Anonymous has rated smithjhon 3.5","459":"Anonymous has rated Emilyxo 8.5","460":"Anonymous has rated leona 10","461":"Anonymous has rated leona 10","462":"Anonymous has rated W0rthlessliar 8","463":"Anonymous has rated Yousamsuck 9","464":"Anonymous has rated Aimeeerobbb 9","465":"Anonymous has rated lauramillerx 10","466":"Anonymous has rated tomwaz 1","467":"Anonymous has rated W0rthlessliar 1","468":"Anonymous has rated W0rthlessliar 1","469":"Anonymous has rated W0rthlessliar 1","470":"Anonymous has rated W0rthlessliar 1"},"latestUser":{"242":"rhiwilliamsx has just signed up","243":"W0rthlessliar has just signed up","244":"rebeccaronan has just signed up"}}

I tried using JSON.stringify and then jQuery.makeArray also to create an array out of it. Then i tried eval('['+string+']') but none of them helped.

I am a newbie in Json couldnt find appropriate support as well.

Regards Himanshu Sharma.

2 Answers 2

3

Use JSON.parse() instead of JSON.stringify.
The fist is JavaScript's equivalent to PHP's json_decode, the latter equivalent to json_encode.

Note: If you're using jQuery.ajax with dataType: 'json', then the parameter in the callback is already a decoded object.

In your code, the PHP "array" is not a JavaScript array, but an object.

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

14 Comments

Ok and I am sorry i didnt noticed we had that datatype option in the ajax too.I will get the Json string from there and how do we access it in the Js?When i alert it,it says an object.So i tried dot notation but was not successfull with that.I need it as an array.
As I said, the object you're passing is not an array, but an object. An example of your callback: function(data) {alert(data.latestRating['456']);}.
Sorry Rob Im bit naive about this.json_encode gives out a json object of an array that is correct i got it.data.latestRating['456'] will give me the element.LatestRating is actually an index here having an array and i want to iterate over in in js so how do we do that?Sorry if this is a stupid question but i am bit confused.
Actually I had done it long time back I remember i used EVAl and got an array.Isnt it possible here Rob?
@techie_28 What do you expect? Update the question with the expected output and relevant code of your current attempt.
|
2

It should already be ready to go if you're using json_encode(), if not, use JSON.parse(). You can also check the PHP headers when you're echoing the data to include:

header('Content-type: application/json');

If it's coming back from an ajax response, just reference the object like so:

success : function(data) {
    console.log(data.latestRating);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.