I am using JSON value to store in javascript array.
My JSON value :
"Users":[{"user_name":"User 1","id":1,"image_url":"/photos/list/missing.png","level":1,"position":{"name":"User ABC","description":"desc"}}]
I'm getting response in alert same as above. Code:
alert("Response : " + $.toJSON(res["Users"]));
O/P : Response : [{"user_name":"User 1","id":1,"image_url":"/photos/list/missing.png","level":1,"position":{"name":"User ABC","description":"desc"}}]
Storing this value as Code:
tempJSON = $.toJSON(res["Users"]);
alert("JSON Array : " + tempJSON.length);
alert("Name : " + tempJSON[0]. user_name);
O/P : JSON Array : 132 -> No. of characters. Name : Undefined. -> Because it can't find value for "user_name"
If I'm Storing this value as Simple String. Code:
tempJSON = [{"user_name":"User 1","id":1,"image_url":"/photos/list/missing.png","level":1,"position":{"name":"User ABC","description":"desc"}}];
alert("JSON Array : " + tempJSON.length);
alert("Name : " + tempJSON[0]. user_name);
Then Getting Exactly,
O/P :
JSON Array : 1 -> No. of Array.
Name : User 1
I don't getting what's going on. Please tell me any solution.
Thanks in advance.
tempJSON[0]
will be the first character of the JSON string. If you need to access the object, you'll need to useres.Users[0]
– steveukx Jul 11 at 7:20