C # formatting JSON strings

Source: Internet
Author: User


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's that? Doesn't matter?
If the data is huge, like this,
{"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" : "8147056167227050270",            "title": "Testing",          &N Bsp "Type": "Product",            "status": "Processed",            "datetime ":" 2014-07-12t21:16:46 ",           " replycontent ":" OK, just test "       },         {            "id": "3a6546f6-49a7-4a17-b679-b3812b12b27e", &nbsp ;           "code": "8147056167227050269",            "title": "I recommend faucet Multiple options available ",           " type ":" Product ",           " status ":" Not processed ",     &NBSP       "datetime": "2014-07-12t18:49:08.933",            "replycontent": "&N Bsp      },        {            "id": "f735e461-ca72-4b44-8d 7b-cd97ac09802f ",           " code ":" 8147056167227050268 ",        &NBS P   "title": "This product is not good, bad use",            "type": "Product",          & nbsp "Status": "Unhandled",            "datetime": "2014-07-12t15:06:19.1",            "replycontent": ""        },        {        &N Bsp   "id": "15926d9d-f469-4921-b01d-4b48ef8bd93d",            "code": " 7141054273018032465 ",           " title ":" JDJBCN ",            "Type": "Service",  &NBSp         "status": "Unhandled",            "datetime": "2014-05-27t01:03:46.477 ",           " replycontent ":"        },        {& nbsp           "id": "1DEBF78F-42B3-4037-B71F-34075EED92BC",            "Code": "4141051277003536211",            "title": "Jdjbxn.x",        & nbsp   "type": "Services",            "status": "Unhandled",            "D Atetime ":" 2014-05-27t00:53:21.18 ",           " replycontent ":"       &NBSP ; },        {            "id": "27593c52-b327-4557-8106-b9156df53909", &NB Sp           "code": "1143051276001357050",            "title": "Ghggg HH ", &nbsp           "type": "Service",            "status": "Unhandled",    &N Bsp       "datetime": "2014-05-27t00:35:05.933",            "replycontent": "&N Bsp      },        {            "id": "040198fc-b466-46c1-89 d8-0514fbde9480 ",           " code ":" 4142053251166372433 ",        &NBS P   "title": "Hello, you know, I don't like the white bathtub",            "type": "Service",        &NBS P   Status: Not processed,            "datetime": "2014-05-25t16:37:43.853",    &nbs P       "replycontent": ""        },        {      &NB Sp     "id": "16185418-d461-4e98-83c3-824eb7e344d6",            "code": "4145058213013197148 ",           " title ":" Hdjbchh ",           " type ": "Services",            "status": "Not processed",            "datetime": "2014 -05-21t01:19:14.903 ",           " replycontent ":" "       },  &nbs P     {            "id": "6c043404-c1db-42e8-adeb-d4880fa7d1b5",    &nbsp ;       "code": "0142051185128085372",            "title": "GHHJDHD",  &nb Sp         "type": "Service",            "status": "Unhandled",      &N Bsp     "datetime": "2014-05-18t12:08:37.997",            "replycontent": "  &N Bsp    },        {            "id": "2dca1a38-a32b-4955-a99c-2ed7 d6de60fA ",           " code ":" 3146050186122030382 ",           " titl E ":" HSIBCN ",           " type ":" Service ",           " status ":" Not Processing ",           " datetime ":" 2014-05-18t12:03:38.913 ",          &NB Sp "Replycontent": "       }   ]}It 's a lot easier. The code does not do error handling and the reader can change it by itself need to import Newtonsoft.json, the version used here is 4.5
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.