This article describes the method of formatting json strings in C#. Share it for your reference, as follows:
Methods to convert Json strings into formatted representations: deserialize strings into objects --> Objects and then serialize them into strings
Using the provided API, download address: /json
Many times we need to use the json string to
{ "status": 1, "sum": 9 }
This way is displayed, and it is often the case when retrieved from the server.
{"status": 1, "sum": 9}
What? It doesn't matter?
If the data is huge, like this
Without formatting, it will be difficult to view. . .
Use the following method to format the result as
{ "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": "Okay, just test" }, { "id": "3a6546f6-49a7-4a17-b679-b3812b12b27e", "code": "8147056167227050269", "title": "I suggest that there are many options for faucets", "type": "product", "status": "Unprocessed", "datetime": "2014-07-12T18:49:08.933", "replycontent": "" }, { "id": "f735e461-ca72-4b44-8d7b-cd97ac09802f", "code": "8147056167227050268", "title": "This product is not very good, not easy to use", "type": "product", "status": "Unprocessed", "datetime": "2014-07-12T15:06:19.1", "replycontent": "" }, { "id": "15926d9d-f469-4921-b01d-4b48ef8bd93d", "code": "7141054273018032465", "title": "jdjbcn", "type": "Serve", "status": "Unprocessed", "datetime": "2014-05-27T01:03:46.477", "replycontent": "" }, { "id": "1debf78f-42b3-4037-b71f-34075eed92bc", "code": "4141051277003536211", "title": "", "type": "Serve", "status": "Unprocessed", "datetime": "2014-05-27T00:53:21.18", "replycontent": "" }, { "id": "27593c52-b327-4557-8106-b9156df53909", "code": "1143051276001357050", "title": "ghggghh", "type": "Serve", "status": "Unprocessed", "datetime": "2014-05-27T00:35:05.933", "replycontent": "" }, { "id": "040198fc-b466-46c1-89d8-0514fbde9480", "code": "4142053251166372433", "title": "Hello, you know, I don't like white bathtubs", "type": "Serve", "status": "Unprocessed", "datetime": "2014-05-25T16:37:43.853", "replycontent": "" }, { "id": "16185418-d461-4e98-83c3-824eb7e344d6", "code": "4145058213013197148", "title": "hdjbchh", "type": "Serve", "status": "Unprocessed", "datetime": "2014-05-21T01:19:14.903", "replycontent": "" }, { "id": "6c043404-c1db-42e8-adeb-d4880fa7d1b5", "code": "0142051185128085372", "title": "ghhjdhd", "type": "Serve", "status": "Unprocessed", "datetime": "2014-05-18T12:08:37.997", "replycontent": "" }, { "id": "2dca1a38-a32b-4955-a99c-2ed7d6de60fa", "code": "3146050186122030382", "title": "hsibcn", "type": "Serve", "status": "Unprocessed", "datetime": "2014-05-18T12:03:38.913", "replycontent": "" } ] }
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 = (jtr); if (obj != null) { StringWriter textWriter = new StringWriter(); JsonTextWriter jsonWriter = new JsonTextWriter(textWriter) { Formatting = , Indentation = 4, IndentChar = ' ' }; (jsonWriter, obj); return (); } else { return str; } }
PS: Regarding json operation, here are some more practical json online tools for your reference:
OnlineJSONCode verification, inspection, beautification and formatting tools:
http://tools./code/json
JSONOnline formatting tools:
http://tools./code/jsonformat
Online XML/JSONConvert each other tools:
http://tools./code/xmljson
jsonCode online formatting/beautification/compression/editing/converting tools:
http://tools./code/jsoncodeformat
OnlinejsonCompression/escaping tools:
http://tools./code/json_yasuo_trans
For more information about C#, you can also view the special topic of this site: "Summary of C# string operation skills》、《Tutorial on the usage of common C# controls》、《Summary of WinForm control usage》、《Summary of thread usage techniques for C# programming》、《Summary of XML file operation skills in C#》、《C# data structure and algorithm tutorial》、《Summary of C# array operation skills"and"Introduction to C# object-oriented programming tutorial》
I hope this article will be helpful to everyone's C# programming.