1

I'm using Django as back-end,in my view.py I return a json object as

return HttpResponse(json.dumps(result))

and in my front-end, in a javascript file I received with

jsonResult = JSON.parse(result);

it is successful cause I can see the variable "jsonResult" if I use the console to check it, just as:

console.log(jsonResult);

The problem is that I want to see its format, as something like this:

{
"tags":{
    "tag":[
               {
               "time":"67",
               "comment":"test 2",
               "id":"18",
               "owner":"xiaoli",
               },
       {
               "time":"30",
               "comment":"",
               "id":"28",
               "owner":"xiaoli",        
       }
        ]
  }
}

How can I do this? Or if I can generate a json file? Thanks.

Vikalp Jain
  • 1,419
  • 1
  • 11
  • 20
Yuqing Wei
  • 289
  • 1
  • 3
  • 12

2 Answers2

0

are you using a python interpreter to do this?

If so you can use "pretty printing" which should display the son file in a nice way.

>>> import json
>>> print json.dumps({'4': 5, '6': 7}, sort_keys=True,
...                  indent=4, separators=(',', ': '))
{
    "4": 5,
    "6": 7
}

Taken from: https://docs.python.org/2/library/json.html

EDIT:

Sorry just seen that you are using JS. How can I pretty-print JSON using JavaScript?

perhaps this helps.

Community
  • 1
  • 1
JP1
  • 731
  • 1
  • 10
  • 27
0

The result of your HTTP request is a string. So you do :

jsonResult = JSON.parse(result);

to parse this string (result) in an object (jsonResult).

So if you want to see this json as a string, you just have to :

console.log(result);

Or add it to an element