简体   繁体   中英

unexpected non-whitespace character after JSON data

string result="12334,23432,3453455";

I am getting this string through Ajax call but it gives me the following error: "unexpected non-whitespace character after JSON data"

When I remove comma's between strings it works fine .How to handle this?. I want to put value in textarea with comma's after the Ajax call

Whatever's outputting that isn't doing so in JSON format, but more like CSV.

A few options:

  1. If you're able, fix the output method to correctly output JSON
  2. Parse the string like a CSV
    eg "12334,23432,3453455".split(',')
  3. Conform the output to JSON first, then parse
    eg JSON.parse("["+"12334,23432,3453455"+"]") (wrap with [] )
  4. Specify dataType:'text' in your $.ajax call.

Options 1-3 of the above would result in [12334,23432,3453455] as a javascript array of numbers, while Option 4 will simply result in "12334,23432,3453455" as a string.

BTW, using JSON.NET , this is what it should result in:

// As an array:
Int32[] ary = new[]{ 12334, 23432, 3453455 };
Console.WriteLine(JsonConvert.SerializeObject(ary));
// [12334,23432,3453455]

// As a string:
String str = "12334,23432,3453455";
Console.WriteLine(JsonConvert.SerializeObject(str));
// "12334,23432,3453455"

Your data has to be parsed by your JSON parser.

If your data is an array , your string should look like:

"[12334,23432,3453455]"

or should it be a string :

"\"12334,23432,3453455\""

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