0

i have this object:

"{"logEntries":[],"value":"-8","text":"Europe","enabled":true}"

how do i get the value of the "value" key in jQuery with minimum code?. in this case "-8".

2
  • You have double quotes inside double quotes. You should wrap it in single quotes, or better remove the quotes entirely, it doesn't need to be a string. Commented Nov 10, 2011 at 15:10
  • @Rocket I can't remove it because this string is a value of a hidden field that is created dynamically by one of Telerik controls. Commented Nov 10, 2011 at 15:17

3 Answers 3

2
var obj = '{"logEntries":[],"value":"-8","text":"Europe","enabled":true}';
obj = $.parseJSON(obj);
console.log(obj.value);

You had double quotes inside double quotes, you can't do that, you need to wrap it in single quotes.

Also, you don't need the quotes here in the first place, just make an object, not a string, so you don't to parse.

var obj = {"logEntries":[],"value":"-8","text":"Europe","enabled":true};
console.log(obj.value);
Sign up to request clarification or add additional context in comments.

Comments

2

I suggest :

var myObject = '{"logEntries":[], "value":"-8", "text":"Europe", "enabled":true}';
alert($.parseJSON(myObject).value);

2 Comments

This will throw an error as $.parseJSON(myObject) is null. Edit: never mind. I see you corrected myObject.
I have tested these 2 lines (copy/paste), and it seems to work.
1
var x = {"logEntries":[],"value":"-8","text":"Europe","enabled":true}

var valueProperty = x.value

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.