简体   繁体   中英

correct malformed JSON data

I'm using $.ajax to get JSON data from a REST API.

The problem is that the responseText I get is malformed so I get SyntaxError: JSON.parse: unexpected non-whitespace character error .

I found out that the problem is that the responseText is something like this:

"433
{"Result":{"Locale":"us","ServiceId":1111,"Name":"name",
"HDLevel":"HD Level 
5a0
Unknown","Category":"News","Subcategory":"ne
5b0
ws"}
}"

...

so it can't be parsed correctly to JSON .

I think I need a way to delete all those strings ( 433 , 5a0 , 5b0 ) and also delete new line characters. But I think I need a general way to delete those strings because there are more like them in my responseText and I can't know all names.

Any ideas on how I can do that and obtain a correct JSON? Thanks

edit :

the service uses JSON as format of the returned data and I'm using:

$.ajax({
    type: 'GET',
    url: URL,
    dataType: 'json', 
    success: function(obj) {

    },
    error: function( jqXHR, textStatus, errorThrown ) {

    },
});

I can't access the service server side so I can't edit any php or other languages issues.

Seems to me more like an unexpected http transfer encoding (chunked). Your actual JSON data is probably fine. Take a look at this question: jquery support Transfer-Encoding:chunked? how

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM