The thing is the sauce thing, some interface returns the result is the JSON data, my side handles some returns to the page JS. But, JS in processing JSON when reported this error: unexpected token illegal, resulting in the abort.
On the internet to find some, I tried some, so summed up.
The result of the return is the sauce thing:
| The code is as follows |
Copy Code |
{"Status": "1", "info": {"date": "2013-12-01", "Content": "1, Test content 1 2, test content 2 3, test content 3 4, test content 4 5, Test content 5 "}}" |
Direct the whole Word will report the above error. But it is not appropriate to remove the line, so the whole thing:
| The code is as follows |
Copy Code |
$response =~ s/r/\r/g; $response =~ s/n/\n/g; |
That is, the return of the result in the change line to "\ r \ n" to JS, and then JS received is RN.
The processing of the JSON class was tested, and nothing was found. It's not surprising that the final deal is really that simple:
Background code to replace the newline character Rn to \ r \ n, the foreground code JS received the character is RN
| The code is as follows |
Copy Code |
| public static string convertfromlisttojson<t> (ilist<t> list, int. Total, String columninfos) where T:class { string[] cols = columninfos.split (New char[]{', '},stringsplitoptions.removeemptyentries); StringBuilder sb = new StringBuilder (300); Sb. Append ("{" Total ":"); Sb. Append (total); Sb. Append ("," Rows ":"); Sb. Append ("["); foreach (T-T in list) { Sb. Append ("{"); foreach (String col in cols) { String name = ' {0} ': ' {1} ', '; String value = Getvalue<t> (T, col); Value = value. Replace ("rn", "\ r \ n"); Sb. Append (String. Format (name, col, value)); } if (cols. Length > 0) { int length = sb. Length; Sb. Remove (length-1, 1); } Sb. Append ("},"); } if (list. Count > 0) { int length2 = sb. Length; Sb. Remove (length2-1, 1); }
Sb. Append ("]"); Sb. Append ("}"); Return SB. ToString (); } private static string getvalue<t> (T T, string pname) where T:class { Type type = T.gettype (); PropertyInfo pinfo = type. GetProperty (pname); if (pinfo!= null) { Object v = pinfo. GetValue (t, NULL); Return v!= null? V.tostring (): ""; } Else { throw new Exception ("No attributes" + pname); }
} |