Parsing malformed JSON with Javascript

后端 未结 7 1630
抹茶落季
抹茶落季 2020-12-21 11:34

I want to parse this content using Javascript. The data looks like this:

{\"ss\":[[\"Thu\",\"7:00\",\"Final\",,\"BAL\",\"19\",\"ATL\",\"20\",,,\"56808\",,\"PRE

相关标签:
7条回答
  • 2020-12-21 11:46

    We are using mootools for stuff like that, but you can do it it plain JavaScript as well: http://www.json.org/js.html.

    0 讨论(0)
  • 2020-12-21 11:56

    Generally speaking, you can use JSON.parse to do this. However, that snippet that you have does not appear to be strictly valid JSON (as seen here: http://jsfiddle.net/yK3Gf/ and also by validating the source JSON here: http://jsonlint.com/).

    So you will either need to parse it by hand, or get nfl.com to fix up their JSON.

    As an alternative, their JSON does parse successfully when using eval(), so you could parse it with something like:

    var parsedData = eval('(' + jsonData + ')');
    

    ...as shown here: http://jsfiddle.net/yK3Gf/1/

    Though be aware that parsing JSON in this way is generally frowned upon (particularly when the data being parsed is being delivered by a third-party source), as it leaves you open to XSS attacks should the data happen to include any executable code inside of it.

    0 讨论(0)
  • 2020-12-21 11:56

    For this specific issue (the empty indexes within the arrays from the JSON response) I did a regex replacement with a lookahead assertion. Considering that request contains the XMLHttpRequest:

    request.responseText.replace(/,(?=,)/gm, ",\"\"")
    

    This will turn ,, into ,"", and will also work in case there are more commas in sequence, so ,,, becomes ,"","",. You can use JSON.parse() afterwards.

    0 讨论(0)
  • 2020-12-21 11:57

    This malformed JSON can be parsed by the dirty-json NPM package (I am the author).

    You can test a demo of the parser here: https://rmarcus.info/dirty-json

    The parser interprets the JSON in your original question as equivalent to the following valid JSON:

    {
        "ss": [
            [
                "Thu",
                "7:00",
                "Final",
                "BAL",
                "19",
                "ATL",
                "20",
                "56808",
                "PRE4",
                "2015"
            ],
            [
                "Thu",
                "7:00",
                "Final",
                "NO",
                "10",
                "GB",
                "38",
                "56809",
                "PRE4",
                "2015"
            ]
        ]
    }
    
    0 讨论(0)
  • 2020-12-21 11:58

    Your main problem is that fact that the JSON your pulling in is malformed or not valid according to RFC 4627.

    What you can do is grab the copy the JSON data and format it using this tool http://www.freeformatter.com/json-formatter.html

    After you have the formatted version then you can use the jQuery ajax call

    $.ajax({
        url: "your-formatted.json",
        dataType: 'json',
    
        success: function (data) {
    
            for (var i = 0; i < data.ss.length; i++) {
                document.write("Day: " + data.ss[i][0]);
                document.write("<br/>");
                document.write("Time: " + data.ss[i][1]);
                document.write("<br/><br/>");
            }
        }
    });
    

    You shouldn't actually use document.write in your application. This is only for example purpose of displaying the data.

    0 讨论(0)
  • 2020-12-21 11:59

    Let's assume you already have a valid JSON String (jsonString) to parse. (If you don't know how to retrieve a String to parse using XMLHttpRequest from the given url you will have to look into that first.)


    With plain JavaScript you will have to add Douglas Crockford's JSON library (or something similar) in order to provide a parsing Function if there is no native implementation:

    var json = json_parse(jsonString) ;
    
    • link

    With a JavaScript library like jQuery this would be

    var json = $.parseJSON(jsonString) ;
    

    Now, traversing the resultant JSON Object is a whole other issue, because you will have to know its structure before you can retrieve specific data. In this particular case -- if it was indeed well formed -- you would have to do the following:

    var data = json.ss ;
    
         for(var i = 0 ; i < data.length ; i++) {
    
              var entry = data[i] ;
    
              var day = entry[0] ; //!! the Arrays seem to have a format where the first entry always contains the data and so forth...
              /* ... */
    
              // then do something with the data bits
    
         }
    
    0 讨论(0)
提交回复
热议问题