Processing of JSON data return line feed

Source: Internet
Author: User

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);
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.