Parsing malformed JSON with Javascript

后端 未结 7 1763
抹茶落季
抹茶落季 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: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.

提交回复
热议问题