user2137186
user2137186

Reputation: 837

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

Upvotes: 0

Views: 3190

Answers (2)

Brad Christie
Brad Christie

Reputation: 101594

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
    e.g. "12334,23432,3453455".split(',')
  3. Conform the output to JSON first, then parse
    e.g. 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"

Upvotes: 1

Kita
Kita

Reputation: 2634

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 astring:

"\"12334,23432,3453455\""

Upvotes: 0

Related Questions