Reputation: 287
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.
Upvotes: 0
Views: 221
Reputation: 3106
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
Upvotes: 2
Reputation: 29267
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
Upvotes: 3