0

I want to parse json retrieved from url in jsp. My url is giving response in json format and now i want to call it in my html page.

My url is giving response as

{"status":1,"msg":"List of Rooms","id":["1","2","3"],"name":["php","Java","myroom4"]}

I am not able to understand what is going wrong.

<script type="text/javascript" charset="utf-8">

    $.get('http:....', function(data, textStatus) {
        alert('Status is '+textStatus);
        alert('JSON data string is: '+data);

        var myJson = JSON.parse(data);
        var myJson = JSON.parse(textStatus);
        var myJsonObj = jsonParse(myJson);

        alert(myJsonObj.msg); 


        }, 'text');

</script>

My url is giving response as

{"status":1,"msg":"List of Rooms","id":["1","2","3"],"name":["php","Java","myroom4"]}
2
  • 2
    Do you get an error ? What is it ? Commented Feb 15, 2012 at 6:28
  • no i am not getting any error. actually i am not getting any response only. Commented Feb 15, 2012 at 6:51

1 Answer 1

5

no need of parsing(JSON.parse)..you can directly say data.msg to get the required response as ajax responses can be directly in the form of JSON objects...

if its not two dimensional

$.getJSON(
          "http....url",
          function(data){

              alert(data.msg);

              });

this should work for you. Tested it here.

Sign up to request clarification or add additional context in comments.

10 Comments

can you please be a bit brief? please write the code if possible
<body> <script type="text/javascript" charset="utf-8"> $.getJSON('http....', function(data, textStatus) { alert('Status is '+textStatus); alert('JSON data string is: '+data); var myJson = JSON.parse(data); var myJsonObj = jsonParse(myJson); alert(myJsonObj.msg); // alert(data.msg); var x=document.getElementById("Roomcount"); alert(x.innerHTML); }, 'text'); </script> <div id="Roomcount"></div> </body> This is my code on which i am working
<html xmlns="w3.org/1999/xhtml" > <head> <title>ABC</title> </head> <script type="text/javascript" charset="utf-8"> $.getJson('http:....', function(data, textStatus) { alert('Status is '+textStatus); alert('JSON data string is: '+data); alert(data.msg); }, 'text'); </script> <body> <div id="Roomcount"></div> </body> </html>. This is also is not working.
edited the answer...again...check out..this should work and you may need to add JQuery to make it work...
what is your output. Can you send me snapshot or something.? I embedded this script in my html page but can not get anything on browser.
|

Your Answer

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.