-1

I have this example and need to understand how to parse the json string from a url rather than inside the html as shown. In essence..I need other easier ways of parsing the text from a url source eg. http://pipes.yahoo.com/pipes/pipe.run?_id=e4e173cf75b0aa1b374b7987398d6091&_render=json&_callback=getYahooFeed

How can jquery be used too? If possible, iteration of the strings/arrays can be demonstrated too.

            <!DOCTYPE html>
        <html>
        <body>
        <h2>Create Object from JSON String</h2>
        <p>
        First Name: <span id="fname"></span><br> 
        Last Name: <span id="lname"></span><br> 
        </p> 
        <script>
        var txt = '{"employees":[' +
        '{"firstName":"John","lastName":"Doe" },' +
        '{"firstName":"Anna","lastName":"Smith" },' +
        '{"firstName":"Peter","lastName":"Jones" }]}';

        obj = JSON.parse(txt);

        document.getElementById("fname").innerHTML=obj.employees[1].firstName 
        document.getElementById("lname").innerHTML=obj.employees[1].lastName 
        </script>
        </body>
        </html>
karto
  • 3,538
  • 8
  • 43
  • 68
  • Where is this URL coming from, and what does it look like ? – adeneo Dec 31 '13 at 14:11
  • is that and ajax response? is the url containing data or what? What exactly is it like? – Akshay Khandelwal Dec 31 '13 at 14:15
  • sorry about url part that confused you. I mean the source of the json file eg. http://pipes.yahoo.com/pipes/pipe.run?_id=e4e173cf75b0aa1b374b7987398d6091&_render=json&_callback=getYahooFeed – karto Dec 31 '13 at 14:42

1 Answers1

0

Take a look at how to extract querystring param and parse it through jQuery's parseJSON

Community
  • 1
  • 1
MSW
  • 62
  • 6