SONAL KASLIWAL
SONAL KASLIWAL

Reputation: 81

Parse response retrieved from url in json

I want to create a parser for login page. I have a url that gives response in json. When not logged in it gives response as :

{"status":0,"msg":"Email is Wrong!"}

and when loggged in it gives :

{"status":1,"msg":"Session is active","session_id":"lp47ngp9hlqtrkunjirqa7ijg5","user_id":"13"}

I have no idea how to start this. Please help... Thanks in advance!

Upvotes: 1

Views: 741

Answers (4)

copenndthagen
copenndthagen

Reputation: 50732

You can use the jQuery parseJSON method to parse the JSON string into Javascript object http://api.jquery.com/jQuery.parseJSON/

Upvotes: -1

KooiInc
KooiInc

Reputation: 122906

Using plain javascript you can use JSON.parse to convert the JSON string to a Javascript Object.

Something like:

var response = JSON.parse([yourJsonString]);
if (response.session_id) {
  // logged in, proceed
} else {
  // not logged in, act accordingly
}

JSON.parse is available in modern browsers. For older browsers you need to include a JSON parser like this one (use json2.js)

Upvotes: 1

lowerkey
lowerkey

Reputation: 8325

Not sure if I'm understanding a real question here, but as I understand it, try using jquery ajax to retrieve your json from the url. In the call to ajax, there'll be a parameter called success, that takes a function with one argument. That argument will be the data retrieved from the url. Simply do obj = eval(data), and your data will be parsed and you can access the status as obj.status.

Upvotes: 1

unludo
unludo

Reputation: 5010

If you use jquery and jquery.ajax/jquery.get, etc., you will receive a response in the form of a javascript object that you may use to react accordingly.

Jquery is an excellent javascript library very very widely used. It's kind of a defacto standard.

Just google it and you will find a lot of information, tutorials, books. In Stack Overflow you will find a lot of material on this topic.

Upvotes: 0

Related Questions