javascript - Problem parsing JSON - Stack Overflow

I encounter problems tring to consume a third party web servive in JSON format. The JSON response from

I encounter problems tring to consume a third party web servive in JSON format. The JSON response from the server kinda looks like this:

{
    "ID":10079,
    "DateTime":new Date(1288384200000),
    "TimeZoneID":"W. Europe Standard Time",
    "groupID":284,
    "groupOrderID":10
}

I use JavaScript with no additional libs to parse the JSON.

//Parse JSON string to JS Object            
var messageAsJSObj = JSON.parse(fullResultJSON);

The parsing fails. A JSON validatior tells me, "new Date(1288384200000)" is not valid.

Is there a library which could help me parse the JSON string?

I encounter problems tring to consume a third party web servive in JSON format. The JSON response from the server kinda looks like this:

{
    "ID":10079,
    "DateTime":new Date(1288384200000),
    "TimeZoneID":"W. Europe Standard Time",
    "groupID":284,
    "groupOrderID":10
}

I use JavaScript with no additional libs to parse the JSON.

//Parse JSON string to JS Object            
var messageAsJSObj = JSON.parse(fullResultJSON);

The parsing fails. A JSON validatior tells me, "new Date(1288384200000)" is not valid.

Is there a library which could help me parse the JSON string?

Share Improve this question edited Nov 22, 2010 at 15:33 j7nn7k asked Nov 22, 2010 at 12:30 j7nn7kj7nn7k 18.6k19 gold badges81 silver badges88 bronze badges 1
  • 5 new Date(1288384200000) is a JavaScript mand that is probably intended to be executed directly. Can't think of a way to do this except var = eval(data); This is not proper JSON, you should plain about this to the third party – Pekka Commented Nov 22, 2010 at 12:33
Add a ment  | 

4 Answers 4

Reset to default 5

Like others have pointed out, it's invalid JSON. One solution is to use eval() instead of JSON.parse() but that leaves you with a potential security issue instead.

A better approach might be to search for and replace these offending issues, turning the data into valid JSON:

fullResultJSON = fullResultJSON.replace(/new Date\((\d+)\)/g, '$1');

You can even go one step further and "revive" these fields into JavaScript Date objects using the second argument for JSON.parse():

var messageAsJSObj = JSON.parse(fullResultJSON, function (key, value) {
    if (key == "DateTime")
        return new Date(value);

    return value;
}); 

Here's an example: http://jsfiddle/AndyE/vcXnE/

Your example is not valid JSON, since JSON is a data exchange technology. You can turn your example into a Javascript object using eval:

var almostJSON = "{
    "ID":10079,
    "DateTime":new Date(1288384200000),
    "TimeZoneID":"W. Europe Standard Time",
    "groupID":284,
    "groupOrderID":10,
}";

and then evaling it:

var myObject = eval('(' + almostJSON + ')');

Then, myObject should hold what you're looking for.

Note that functions are not allowed in JSON because that could promise security.

try var obj = eval('(' + fullResultJSON + ')'); and you'll have the object like Pekka said. Don't forget to use the extra '()' though. And indeed json should have both property and value enclosed in quotes.

Parsing fails because all you can parse in a json object are null, strings, numbers, objects, arrays and boolean values so new Date(1288384200000), cannot be parsed

You have also another problem, last property shouldn't have the trailing ma.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742299074a4417697.html

相关推荐

  • javascript - Problem parsing JSON - Stack Overflow

    I encounter problems tring to consume a third party web servive in JSON format. The JSON response from

    17小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信