0

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
4
  • possible duplicate of How to parse JSON in JavaScript Commented 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). Commented Jul 9, 2012 at 9:07
  • I tried var obj = jQuery.parseJSON(JSON); It showing Object expected. But i tried with double quotes. Commented Jul 9, 2012 at 9:13
  • this is working obj = JSON.parse(json); Thank you Felix and you all. Commented Jul 9, 2012 at 9:22

2 Answers 2

3

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

I tried this. But i am getting error as Object does not support this property or method.
2

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
answered Jul 9, 2012 at 9:09

1 Comment

I tried this. But i am getting error as Object does not support this property or method.

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.