-2

I have posted my html response.Can anybody help me with me parsing the following response?

<?xml version="1.0" encoding="utf-8"?>

<string xmlns="http://localhost:53179/hdfcmobile">
    {"Status":"True","Data":[{"Loginstatus":"Success","agentid":1004}]}
</string>
user751828
  • 85
  • 4
  • 14

4 Answers4

2

That is not an HTML response. It is a JSON response.

You can parse it using one of the many JSON parser libraries. There is a comprehensive listing at http://www.json.org.


UPDATE

For the new version of your Question, what you appear to have is an XML document that has JSON embedded in an XML element.

You need to use an XML parser to extract the text contents of the string element, and then use a JSON parser to parse that text.

Why on earth would anyone think it was a good idea to mix XML and JSON like that??

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

You response is JSON string. In order to use it, you should convert it to JavaScript object. eval function can be used for that purpose:

var response = '{"Status":"True","Data":[{"Loginstatus":"Success","agentid":1004}]}';
eval('var a='+response);
alert("Status = " + a.Status);
alert("Data.Loginstatus = " + a.Data[0].Loginstatus);
alert("Data.agentid = " + a.Data[0].agentid);

UPDATE

Question has been updated since I left the answer, so here is addition to my answer :). In order to extract JSON string from the obtained XML response, you can use regular expression "<string[^>]*>(.*?)<\/string>" like this:

var responseText = '<?xml version="1.0" encoding="utf-8"?>

<string xmlns="http://localhost:53179/hdfcmobile">
    {"Status":"True","Data":[{"Loginstatus":"Success","agentid":1004}]}
</string>';

var oRegExp = new RegExp("<string[^>]*>(.*?)<\/string>", "ig");
var matches = oRegExp.exec(responseText);
var response = matches[1];

After that you can use the code written above to convert response to JavaScript object.

bhovhannes
  • 5,511
  • 2
  • 27
  • 37
  • Use `JSON.parse()` instead of `eval`. – sachleen Oct 11 '12 at 05:43
  • @sachleen, you are right, `JSON.parse()` is more secure, but that method is not available for older browsers. According to developer.mozilla.org, `JSON.parse()` is included since JavaScript 1.7 only. – bhovhannes Oct 11 '12 at 05:48
  • Thanks for ur help..I tried with it..but I am not getting the json values – user751828 Oct 11 '12 at 06:03
  • It is **evil** to "parse" xml with regexes. One insignificant change to the XML rendering and the regex will break. (I hear Tony the Pony approaching ... hold your breath everyone :-) ) – Stephen C Oct 11 '12 at 06:12
  • @user751828 Which is your environment? I tried the code above in Firefox Javascript console and it worked (I saw values in alert boxes) – bhovhannes Oct 11 '12 at 06:12
  • http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Stephen C Oct 11 '12 at 06:14
  • @Stephen-C it is not less evil to send JSON response via XML :-)) evil x2 is also evil, isn't it? :D At least regex is nice... – bhovhannes Oct 11 '12 at 06:16
  • @Stephen-C, thanks for interesting link. I think it is not parsing here. All that is needed is to extract enveloped JSON string, which is content of documentElement of received XML document – bhovhannes Oct 11 '12 at 06:20
  • @bhovhannes - the same principle applies whether you call this "parsing" or "extracting information". And there are differing degrees evil here ... and probably different parties perpetrating it. – Stephen C Oct 11 '12 at 10:25
  • @Stephen-C - of course, but I don't see the sense in utilizing XML parser just to get content of documentElement (in this case only that is needed). At least it will affect performance. Yes, regexes should not be used for HTML parsing, because it is just impossible due to HTML complexity, but there are specific simple cases, when regexes can be used (they are invented to extract and match information). It is so simple XML here (only 1 root tag), that the big evil can come only from untrusted JSON data. – bhovhannes Oct 11 '12 at 10:45
0

It's just json data. You should be able to read it into an object:

var JSONtext = '{"Status":"True","Data":[{"Loginstatus":"Success","agentid":1004}]}'
var myObject = JSON.parse( JSONtext );

As to why you're getting a json object inside xml as a response, that's another matter.. generally your responses should be either json OR xml

Ben D
  • 14,321
  • 3
  • 45
  • 59
0

That looks like XML respose with JSON inside one of the nodes - rather than just a JSON response. You'll probably extract the JSON string out of that node, and then parse it using JSON.parse(extracted_json)

techfoobar
  • 65,616
  • 14
  • 114
  • 135