-1

I am developing a site on Django framework which returns a json response to be used jquery datatable. Datatable requires input to be either javascript object or array of arrys so i need to convert this on either server side or client side to javascript object or array.

Here is the related documentation for the datatable.

DataTables AJAX source example DataTables AJAX source example - array of objects as a data source

[
    {
        "pk": 7,
        "model": "softwareapp.software",
        "fields": {
            "city": "miami",
            "submitted_by": [],
            "description": "test",
            "title": "test",
            "zipcode": "test",
            "rating_votes": 0,
            "state": "fl",
            "address": "test",
            "rating_score": 0,
            "business_size": [
                5
            ],
            "slug": "test",
            "developer": "test"
        }
    },
    {
        "pk": 8,
        "model": "softwareapp.software",
        "fields": {
            "city": "",
            "submitted_by": [],
            "description": "",
            "title": "test2",
            "zipcode": "",
            "rating_votes": 0,
            "state": "",
            "address": "",
            "rating_score": 0,
            "business_size": [
                5
            ],
            "slug": "test2",
            "developer": ""
        }
    },
    {
        "pk": 10,
        "model": "softwareapp.software",
        "fields": {
            "city": "",
            "submitted_by": [],
            "description": "",
            "title": "test3",
            "zipcode": "",
            "rating_votes": 0,
            "state": "",
            "address": "",
            "rating_score": 0,
            "business_size": [
                6
            ],
            "slug": "test3",
            "developer": ""
        }
    }
]
shaytac
  • 3,789
  • 9
  • 36
  • 44

2 Answers2

0

Most browsers support JSON.parse(), which is defined in ECMA-262 5th Edition (the specification that JS is based on). Its usage is simple:

var json = '{"result":true,"count":1}',
obj = JSON.parse(json);

alert(obj.count);

For the browsers that don't you can implement it using json2.js.

Arpit
  • 953
  • 7
  • 11
0

On the client you could use:

JSON.parse(json)

"json" being the JSON string .

Or if you're also using jQuery to do the AJAX request it will do the deserialization for you in the "success" handler.

        $.ajax({
        type: "GET",
        url: "your url",
        dataType: "json",
        contentType: "application/json",
        success: function (e) {
           // the value of "e" should be a javascript object or array depending on the    response

        }
    });