Parsing JSON Issues

I am encountering tring issues to use a third-party web service in JSON format. The JSON response from the server is as follows:

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

I use JavaScript without additional libraries to parse JSON.

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

Parsing is not performed. JSON validatior tells me that the "new date (1288384200000)" is not valid.

Is there a library that can help me parse a JSON string?

+3
source share
4 answers

As others have pointed out, this is invalid JSON. One solution is to use eval()instead JSON.parse(), but this leaves you with a potential security issue.

, JSON:

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

"" JavaScript Date, JSON.parse():

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

    return value;
}); 

: http://jsfiddle.net/AndyE/vcXnE/

+5

JSON, JSON - . Javascript, eval:

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

eval:

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

myObject , .

, JSON, .

+3

var obj = eval ('(' + fullResultJSON + ')'); , . '()'. , json , , .

0

Analysis is not possible because all you can parse in a json object is null, strings, numbers, objects, arrays and booleans, therefore new Date(1288384200000)it cannot be parsed

You also have another problem, the last property should not have a trailing comma.

0
source

Source: https://habr.com/ru/post/1776211/


All Articles