1

I am unable to figure out what is the problem with displaying json data..below is the code

var xhrGet1 = dojo.xhrGet({
       url: "Page/",
       handleAs: "json",
       handle: function(response)
       {
       dojo.byId('json-data').innerHTML = response.questions[0];
       }
       });

Html

<div id='json-data'></div>

And my json file looks like this

 {
        "Info": {
            "PURPOSE": "....  ",
            },
        "questions": [
            {
                "ID": 1,
                "Question": "User ID",
                "Information": "",

            }, {
                "ID": 2,
                "Question": "Name",
                "Information": "",

            }
        ],

so on...any ideas??

Newbie
  • 361
  • 2
  • 15
  • 33

2 Answers2

3

The property handleAs : "json" in your xhr call makes the incoming json automatically eval'ed to javascript objects. So, you have to convert your javascript object back to string using JSON.stringify. e.g. :

dojo.byId('json-data').innerHTML = JSON.stringify(response.questions[0]);

You can also use dojo.toJson for the same purpose. It uses json.stringify but has the benefit of having a second argument ("prettyprint"), allowing you to pretty-print out of the box, like this :

dojo.byId('json-data').innerHTML = dojo.toJson(response.questions[0], true);
Philippe
  • 6,703
  • 3
  • 30
  • 50
  • You're welcome... if any solution is what you are looking for, you may consider accepting it by ticking the tick sign just below the answer score... – Philippe Mar 16 '12 at 17:43
0

wrap your JSON with PRE and CODE tags.

So:

dojo.byId('json-data').innerHTML = "<pre>code>" + response.questions[0] + "</code></pre>";

Also see: Display JSON as HTML for some libraries that can help you pretty-format your JSON when rendering in the browser

Community
  • 1
  • 1
Vijay Agrawal
  • 3,751
  • 2
  • 23
  • 25