1

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!

Chuck Norris
  • 15,207
  • 15
  • 92
  • 123
SONAL KASLIWAL
  • 71
  • 1
  • 3
  • 7
  • possible duplicate of [how to parse json in javascript](http://stackoverflow.com/questions/4935632/how-to-parse-json-in-javascript) – Matt Feb 17 '12 at 11:10

4 Answers4

1

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.

lowerkey
  • 8,105
  • 17
  • 68
  • 102
1

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)

KooiInc
  • 119,216
  • 31
  • 141
  • 177
  • Using Bing and JSON $.getJSON('url', function (data) { alert(data.status); // 1 alert(data.msg); // List of rooms });

    This is the code on which i am working

    – SONAL KASLIWAL Feb 17 '12 at 11:34
0

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.

unludo
  • 4,912
  • 7
  • 47
  • 71
-1

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

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
copenndthagen
  • 49,230
  • 102
  • 290
  • 442