Hi how to Parse JSON string using JQuery or Javascript??
I have the JSON string like below format.
var JSON = "{ "UserID":"1","ClientID":"1","UserName":"User1"}"
I wanna to parse this JSON string. so that i can get
var UserID = 1
var ClientID = 1
UserName = User1
Can anybody help me out..
Thanks.
Felix Kling
820k181 gold badges1.1k silver badges1.2k bronze badges
asked Jul 9, 2012 at 9:04
-
possible duplicate of How to parse JSON in JavaScriptFelix Kling– Felix Kling2012年07月09日 09:06:24 +00:00Commented Jul 9, 2012 at 9:06
-
Parse the JSON into a JavaScript object and extract the desired data. Read Working with Objects if you don't know how to access properties of objects (JavaScript basics).Felix Kling– Felix Kling2012年07月09日 09:07:14 +00:00Commented Jul 9, 2012 at 9:07
-
I tried var obj = jQuery.parseJSON(JSON); It showing Object expected. But i tried with double quotes.watraplion– watraplion2012年07月09日 09:13:59 +00:00Commented Jul 9, 2012 at 9:13
-
this is working obj = JSON.parse(json); Thank you Felix and you all.watraplion– watraplion2012年07月09日 09:22:40 +00:00Commented Jul 9, 2012 at 9:22
2 Answers 2
First of all, if you execute the JSON variable you have there, should give you a syntax error because you need to escape the double quotes, such as:
var JSON = "{ \"UserID\":\"1\",\"ClientID\":\"1\",\"UserName\":\"User1\"}";
or simply use single quotes to create the string
var JSON = '{ "UserID":"1","ClientID":"1","UserName":"User1"}';
Then you can just parse it using jQuery.parseJSON()
var obj = jQuery.parseJSON(JSON);
obj.UserID == 1; // true
answered Jul 9, 2012 at 9:07
1 Comment
watraplion
I tried this. But i am getting error as Object does not support this property or method.
Be wary of unescaped quotation marks in that string. I changed the outer quotes to single quotes.
var obj = jQuery.parseJSON('{ "UserID":"1","ClientID":"1","UserName":"User1"}')
var UserID = obj.UserID
var ClientID = obj.ClientID
var UserName = obj.UserName
1 Comment
watraplion
I tried this. But i am getting error as Object does not support this property or method.
lang-js