Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
85
rated 0 times [  87] [ 2]  / answers: 1 / hits: 28189  / 12 Years ago, fri, january 25, 2013, 12:00:00

My controller has a method that is returning string representation of a jsonArray as



jsonArray.toString()



Now following is the ajax method



function loadPropertyFile(url) {
$.ajax({
type: GET,
url: url,
dataType: text,
success: function(response){
var obj = jQuery.parseJSON(response);
alert(obj);
}
});


}



Here the variable obj after parsing comes out to be



[{portal.home:Home},{displaytag.tracking.id:Item ID},{displaytag.tracking.itemName:Item Name},{displaytag.tracking.itemType:Type}]


Now I want to access the values from the keys in js



ie. I want to access the value of key displaytag.tracking.id



Problem is when I am doing console.log(obj[0][portal.home]); It is giving me error TypeError: obj[0] is undefined



What shall I do ?


More From » jquery

 Answers
3

First you need to parse the JSON string into JavaScript object, and then access the required property:



var obj = JSON.parse(json);
console.log(obj[0][portal.home]);


In older browsers which do not have native JSON support, you should use something like Crockford's json2.js, which will give you one; please don't use eval() on JSON, as it can lead to pretty bad things all around.


[#80631] Wednesday, January 23, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
aden

Total Points: 369
Total Questions: 100
Total Answers: 83

Location: Australia
Member since Tue, Oct 19, 2021
3 Years ago
;