Method analysis of how to format JSON strings in C #

Source: Internet
Author: User
This article mainly introduces C # format JSON string method, combined with instance form Analysis C # for JSON string formatting principles, steps and specific implementation skills, the need for friends can refer to the following

The example in this article describes how C # formats a JSON string. Share to everyone for your reference, as follows:

Converts a JSON string into a formatted representation: string deserialized into an object and the object is then serialized as a string

Using the API provided by Newtonsoft.json,

Many times we need to use a JSON string to


{  "status": 1,  "sum": 9}

This is the way it appears, and it's often the case when it's retrieved from the server.


{"Status": 1, "Sum": 9}

What the? Doesn't matter?

If the data is huge, like this,

Copy the Code code as follows:

{"Status": 1, "TotalCount": 2, "list": [{"id": "2305b1e2-4e31-4fd3-8eb6-db57641914df", "Code": "8147056167227050270", " Title ":" Testing "," type ":" Product "," status ":" Processed "," datetime ":" 2014-07-12t21:16:46 "," replycontent ":" OK, just test "},{" id ":" 3a6546f6-49a7-4a17-b679-b3812b12b27e "," Code ":" 8147056167227050269 "," title ":" I suggest that the faucet has a variety of options "," type ":" Product "," status " : "Unhandled", "datetime": "2014-07-12t18:49:08.933", "replycontent": ""},{"id": "f735e461-ca72-4b44-8d7b-cd97ac09802f", " Code ":" 8147056167227050268 "," title ":" This product is not good, bad use "," type ":" Product "," status ":" Unhandled "," datetime ":" 2014-07-12t15:0 6:19.1 "," replycontent ":" "},{" id ":" 15926d9d-f469-4921-b01d-4b48ef8bd93d "," Code ":" 7141054273018032465 "," title ":" JDJBCN "," type ":" Service "," status ":" Unhandled "," datetime ":" 2014-05-27t01:03:46.477 "," replycontent ":"},{"id": " 1DEBF78F-42B3-4037-B71F-34075EED92BC "," Code ":" 4141051277003536211 "," title ":" Jdjbxn.x "," type ":" Service "," status ":" Unhandled "," datetime ":" 2014-05-27t00:53:21.18 "," replycontent ":" ""},{"id": "27593c52-b327-4557-8106-b9156df53909", "code ":" 1143051276001357050 "," title":" Ghggghh "," type ":" Service "," status ":" Unhandled "," datetime ":" 2014-05-27t00:35:05.933 "," replycontent ":"},{"id": " 040198fc-b466-46c1-89d8-0514fbde9480 "," Code ":" 4142053251166372433 "," title ":" Hello, you know, I do not like the white bathtub "," type ":" Service "," Status ": Unhandled", "datetime": "2014-05-25t16:37:43.853", "replycontent": ""},{"id": "16185418- D461-4e98-83c3-824eb7e344d6 "," Code ":" 4145058213013197148 "," title ":" Hdjbchh "," type ":" Service "," status ":" Unhandled "," DateTime ":" 2014-05-21t01:19:14.903 "," replycontent ":" "},{" id ":" 6c043404-c1db-42e8-adeb-d4880fa7d1b5 "," Code ":" 0142051185128085372 "," title ":" GHHJDHD "," type ":" Service "," status ":" Unhandled "," datetime ":" 2014-05-18t12:08:37.997 "," Replycontent ":" "},{" id ":" 2DCA1A38-A32B-4955-A99C-2ED7D6DE60FA "," Code ":" 3146050186122030382 "," title ":" Hsibcn "," Type ": Service", "status": "Unhandled", "datetime": "2014-05-18t12:03:38.913", "replycontent": ""}]}

If it is not formatted, it will be difficult to see ...

Use the following method to format the result as such

{"Status": 1, "TotalCount": 2, "list": [{"id": "2305b1e2-4e31-4fd3-8eb6-db57641914df", "Code": "8147056      167227050270 "," title ":" Testing "," type ":" Product "," status ":" Processed "," datetime ":" 2014-07-12t21:16:46 ", "Replycontent": "OK, just test"}, {"id": "3a6546f6-49a7-4a17-b679-b3812b12b27e", "Code": "8147056167227050      269 "," title ":" I suggest that the faucet has a variety of options "," type ":" Product "," status ":" Unhandled "," datetime ":" 2014-07-12t18:49:08.933 ",      "Replycontent": ""}, {"id": "f735e461-ca72-4b44-8d7b-cd97ac09802f", "Code": "8147056167227050268", "title": "This product is not good, bad use", "type": "Product", "status": "Unhandled", "datetime": "2014-07-12t15:06:19.1", "Repl Ycontent ":" "}, {" id ":" 15926d9d-f469-4921-b01d-4b48ef8bd93d "," Code ":" 7141054273018032465 "," titl     E ":" JDJBCN "," type ":" Service "," status ":" Unhandled "," datetime ":" 2014-05-27t01:03:46.477 "," replycontent ":" " }, {"id": "1DEBF78F-42B3-4037-B71F-34075EED92BC "," Code ":" 4141051277003536211 "," title ":" Jdjbxn.x "," type ":" Service ", "Status": "Unhandled", "datetime": "2014-05-27t00:53:21.18", "replycontent": ""}, {"id": "27593c52-b32 7-4557-8106-b9156df53909 "," Code ":" 1143051276001357050 "," title ":" Ghggghh "," type ":" Service "," status ": "Unhandled", "datetime": "2014-05-27t00:35:05.933", "replycontent": ""}, {"id": "040198fc-b466-46c1-89d8- 0514fbde9480 "," Code ":" 4142053251166372433 "," title ":" Hello, you know, I do not like the white bathtub "," type ":" Service "," status ":" Not in "," "datetime": "2014-05-25t16:37:43.853", "replycontent": ""}, {"id": "16185418-d461-4e98-83c3-824 Eb7e344d6 "," Code ":" 4145058213013197148 "," title ":" Hdjbchh "," type ":" Service "," status ":" Unhandled "," Da      Tetime ":" 2014-05-21t01:19:14.903 "," replycontent ":" "}, {" id ":" 6c043404-c1db-42e8-adeb-d4880fa7d1b5 ", "Code": "0142051185128085372 "," title ":" GHHJDHD "," type ":" Service "," status ":" Unhandled "," datetime ":" 2014-05-18t12:08:37.997 ",      "Replycontent": ""}, {"id": "2DCA1A38-A32B-4955-A99C-2ED7D6DE60FA", "Code": "3146050186122030382", "title": "HSIBCN", "type": "Service", "status": "Unhandled", "datetime": "2014-05-18t12:03:38.913", "Replyco Ntent ":" "}]}

The implementation code is as follows:

private string convertjsonstring (String str) {  //format JSON string  jsonserializer serializer = new Jsonserializer ();  TextReader tr = new StringReader (str);  JsonTextReader JTR = new JsonTextReader (tr);  Object obj = serializer. Deserialize (JTR);  if (obj! = null)  {    StringWriter textWriter = new StringWriter ();    JsonTextWriter jsonwriter = new JsonTextWriter (textWriter)    {      formatting = formatting.indented,      Indentation = 4,      IndentChar = '    };    Serializer. Serialize (Jsonwriter, obj);    return textwriter.tostring ();  }  else  {    return str;}  }

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.