0

I got

[[["汽車","car","Qìchē",""]],[["名詞",["汽車","車","轎車","車輛","車廂"],[["汽車",["car","automobile","auto"],,0.26497361],["車",["car","vehicle","lathe","machine","rook","turn"],,0.21967085],["轎車",["car","bus"],,0.020115795],["車輛",["vehicle","car"],,0.013611027],["車廂",["car"],,0.0042828997]]]],"en",,[["汽車",[4],0,0,1000,0,1,0]],[["car",4,[["汽車",1000,0,0],["車",0,0,0],["轎車",0,0,0],["車輛",0,0,0],["車廂",0,0,0]],[[0,3]],"car"]],,,[["en"]],27]

this from google translator However I tried

JSON.parse(xhr.responseText);

It return an error Unexpected token

4
  • 2
    Before answering, what you posted here is already parsed data, while JSON.parse expects a string (a quote delimited value). Commented Jun 4, 2013 at 7:01
  • Because It types is string Commented Jun 4, 2013 at 7:04
  • 1
    WHere is this data coming from? As one of tha answers said, it's not valid JSON because of ,,0.21967085. If it's an API, you need to report the bug to them. Commented Jun 4, 2013 at 7:10
  • 1
    Two things to notice here. This is a valid JS object (array, specifically), but not a valid json. That said, if this is a string then you should write it surrounded by quotes (just for clarity). Finally, if what you want is parse it to a js (not json) object, then an eval(xhr.responseText). Eval should be always avoided, but i use it here for brevity. Commented Jun 4, 2013 at 7:13

2 Answers 2

1

The problem is that this string contains multiple commas making your json invalid.

You could try to replace it for a single one before parsing

var x = '[[["汽車","car","Qìchē",""]],[["名詞",["汽車","車","轎車","車輛","車廂"],[["汽車",["car","automobile","auto"],,0.26497361],["車",["car","vehicle","lathe","machine","rook","turn"],,0.21967085],["轎車",["car","bus"],,0.020115795],["車輛",["vehicle","car"],,0.013611027],["車廂",["car"],,0.0042828997]]]],"en",,[["汽車",[4],0,0,1000,0,1,0]],[["car",4,[["汽車",1000,0,0],["車",0,0,0],["轎車",0,0,0],["車輛",0,0,0],["車廂",0,0,0]],[[0,3]],"car"]],,,[["en"]],27]'
    .replace(/,{2,}/g, ",") // 2 or more replace for 1

JSON.parse(x);

Or if you have access to whatever is sending this string fix the output.

Sign up to request clarification or add additional context in comments.

1 Comment

I think he said it's coming from Google Translate.
0

First you should remove an extra [] brackets by replacing that.

ex,

[["汽車","car","Qìchē",""]]

should be:

["汽車","car","Qìchē",""]

EDIT: you can refer to this answer: Parse Google Translate Json C#

You should try:

var str = '[[["汽車","car","Qìchē",""]],[["名詞",["汽車","車","轎車","車輛","車廂"],[["汽車",["car","automobile","auto"],,0.26497361],["車",["car","vehicle","lathe","machine","rook","turn"],,0.21967085],["轎車",["car","bus"],,0.020115795],["車輛",["vehicle","car"],,0.013611027],["車廂",["car"],,0.0042828997]]]],"en",,[["汽車",[4],0,0,1000,0,1,0]],[["car",4,[["汽車",1000,0,0],["車",0,0,0],["轎車",0,0,0],["車輛",0,0,0],["車廂",0,0,0]],[[0,3]],"car"]],,,[["en"]],27]';

var objstr = $.parseJSON(str);

1 Comment

There's nothing wrong with double brackets, it's just an array containing another array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.