2

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 ?

asked Jan 25, 2013 at 9:39
4
  • Is it a string, or do you have an actual javascript object. And this should be trivial to figure out either way ? Commented Jan 25, 2013 at 9:40
  • Trivial question, and not clearly formed. Commented Jan 25, 2013 at 9:43
  • Question edited. Please see Commented Jan 25, 2013 at 10:00
  • Your json string should be like this '[{"portal.home":"Home"},{"displaytag.tracking.id":"Item ID"},{"displaytag.tracking.itemName":"Item Name"},{"displaytag.tracking.itemType":"Type"}]' remove the double quotes at front and at end use single quotes Commented Jan 25, 2013 at 10:09

4 Answers 4

8

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.

TML
13k3 gold badges42 silver badges47 bronze badges
answered Jan 25, 2013 at 9:41
Sign up to request clarification or add additional context in comments.

Comments

2

Use $.parseJSON (or JSON.parse in modern browsers) to convert your string into a Javascript object:

var json = '[{"portal.home":"Home"},{"displaytag.tracking.id":"Item ID"},{"displaytag.tracking.itemName":"Item Name"},{"displaytag.tracking.itemType":"Type"}]';
var object = $.parseJSON(json);

In your case your JSON string will create an array, so you will need to get the object at the correct index:

var portalHomeValue = object[0]["portal.home"];
answered Jan 25, 2013 at 9:40

5 Comments

For old browsers you can use JSON2. If you don't use jQuery.
This json object is coming after parsing var obj = jQuery.parseJSON(response);
@bhuvan if its an object then use obj[0]['portal.home']
@bhuvan If I paste the following into the console I don't get an error and "Home" is returned: var json = '[{"portal.home":"Home"},{"displaytag.tracking.id":"Item ID"},{"displaytag.tracking.itemName":"Item Name"},{"displaytag.tracking.itemType":"Type"}]'; var object = $.parseJSON(json); console.log(object[0]["portal.home"])
nope.. The actual response coming from controller is "{"jsonData":"[{\"portal.home\":\"Home\"},{\"displaytag.tracking.id\":\"Item ID\"},{\"displaytag.tracking.itemName\":\"Item Name\"},{\"displaytag.tracking.itemType\":\"Type\"}]","jsonDataMake":null,"jsonDataModel":null,"jsonDataSoftwareVersion":null,"jsonItemProperties":null,"jsonPositionMethods":null}". After var object = $.parseJSON(json); the response becomes '[{"portal.home":"Home"},{"displaytag.tracking.id":"Item ID"},{"displaytag.tracking.itemName":"Item Name"},{"displaytag.tracking.itemType":"Type"}]'
0

In case you have the JSON directly in your javascript source (which I don't assume, but I'm adding this for others), you can just remove the quotes, and javascript creates an object based on it:

var obj = [{"portal.home":"Home"},{"displaytag.tracking.id":"Item ID"},{"displaytag.tracking.itemName":"Item Name"},{"displaytag.tracking.itemType":"Type"}];
console.log(obj[0]["portal.home"]);
answered Jan 25, 2013 at 9:43

1 Comment

well, following your logic, it is better to use console.log("Home") as a solution.
0

You can directly access by following

var data = JSON.parse('[{"Item_Number":"M71118LHB","Description":"MENS ONESIE"}]');
var desc = data[0].Description;
console.log(desc);
answered Apr 3, 2018 at 13:52

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.