iJade
iJade

Reputation: 23801

Unexpected Token error due to u in json data

I created json data using python code.Here is the sample json data created

[{'signedDate': u'2013-05-25T04:13:12.2000000Z', 'name': u'Ravi Shastri', 'roleName': u'Firigner'}]

And here is the python code

records = []
for i in env:
    record1 = {"name":t[0]['name'],"signedDate":t[0]['signedDateTime'],"roleName":t[0]['roleName']}
    records.append(record1)
return str(records)

And i'm binding this json data to a grid view jquery plugin.Here is the jquery ajax code. Note: responseText is the json data

$(document).ready(function() {
            $.ajax({
                        type: "GET",
                        url: "/getdetails",
            dataType: "json",
                        success: function (responseText) {
                            alert(responseText);
                                    $("#exampleGrid").simplePagingGrid({
                            columnNames: ["name", "signedDate ($)", "roleName"],
                            columnKeys: ["name", "signedDate", "roleName"],
                            columnWidths: ["50%", "25%", "25%"],
                            data: responseText
                    });
                        },
                        error: function (xhr, errorType, exception) {  
                            var errorMessage = exception || xhr.statusText;  
                            alert("There was an error creating your contact: " + errorMessage);
                        }
                });
        });

I get an error SyntaxError: Unexpected token....i think it may be due to u in json data.Is there any way to remove u from json data.

Upvotes: 0

Views: 825

Answers (1)

Blender
Blender

Reputation: 298196

That's not JSON. It's just the string representation of a Python dictionary, which happens to look like JSON. To make a valid JSON string, you need to encode your object:

import json

...

return json.dumps(records)

Upvotes: 2

Related Questions