對象再序列化為字符..."/>
溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C#格式化json字符串的方法分析

發(fā)布時(shí)間:2020-09-23 11:34:55 來源:腳本之家 閱讀:248 作者: 欄目:編程語言

本文實(shí)例講述了C#格式化json字符串的方法。分享給大家供大家參考,具體如下:

將Json字符串轉(zhuǎn)化成格式化表示的方法: 字符串反序列化為對象-->對象再序列化為字符串

使用Newtonsoft.Json提供的API,下載地址:http://www.newtonsoft.com/json

很多時(shí)候我們需要將json字符串以

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

這種方式顯示,而從服務(wù)端取回來的時(shí)候往往是這樣

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

什么?無所謂?

如果數(shù)據(jù)很龐大的時(shí)候,比如這樣

復(fù)制代碼 代碼如下:
{"status":1,"totalcount":2,"list":[{"id":"2305b1e2-4e31-4fd3-8eb6-db57641914df","code":"8147056167227050270","title":"testing","type":"產(chǎn)品","status":"已處理","datetime":"2014-07-12T21:16:46","replycontent":"好的,只是測試"},{"id":"3a6546f6-49a7-4a17-b679-b3812b12b27e","code":"8147056167227050269","title":"我建議龍頭有多種選配方式","type":"產(chǎn)品","status":"未處理","datetime":"2014-07-12T18:49:08.933","replycontent":""},{"id":"f735e461-ca72-4b44-8d7b-cd97ac09802f","code":"8147056167227050268","title":"這個(gè)產(chǎn)品不怎么好,不好用","type":"產(chǎn)品","status":"未處理","datetime":"2014-07-12T15:06:19.1","replycontent":""},{"id":"15926d9d-f469-4921-b01d-4b48ef8bd93d","code":"7141054273018032465","title":"jdjbcn","type":"服務(wù)","status":"未處理","datetime":"2014-05-27T01:03:46.477","replycontent":""},{"id":"1debf78f-42b3-4037-b71f-34075eed92bc","code":"4141051277003536211","title":"jdjbxn.x","type":"服務(wù)","status":"未處理","datetime":"2014-05-27T00:53:21.18","replycontent":""},{"id":"27593c52-b327-4557-8106-b9156df53909","code":"1143051276001357050","title":"ghggghh","type":"服務(wù)","status":"未處理","datetime":"2014-05-27T00:35:05.933","replycontent":""},{"id":"040198fc-b466-46c1-89d8-0514fbde9480","code":"4142053251166372433","title":"你好,你知道啦,我不喜歡白色浴缸","type":"服務(wù)","status":"未處理","datetime":"2014-05-25T16:37:43.853","replycontent":""},{"id":"16185418-d461-4e98-83c3-824eb7e344d6","code":"4145058213013197148","title":"hdjbchh","type":"服務(wù)","status":"未處理","datetime":"2014-05-21T01:19:14.903","replycontent":""},{"id":"6c043404-c1db-42e8-adeb-d4880fa7d1b5","code":"0142051185128085372","title":"ghhjdhd","type":"服務(wù)","status":"未處理","datetime":"2014-05-18T12:08:37.997","replycontent":""},{"id":"2dca1a38-a32b-4955-a99c-2ed7d6de60fa","code":"3146050186122030382","title":"hsibcn","type":"服務(wù)","status":"未處理","datetime":"2014-05-18T12:03:38.913","replycontent":""}]}

如果沒有格式化,將很難查看。。。

使用下面的方法,將結(jié)果格式化為這樣

{
  "status": 1,
  "totalcount": 2,
  "list": [
    {
      "id": "2305b1e2-4e31-4fd3-8eb6-db57641914df",
      "code": "8147056167227050270",
      "title": "testing",
      "type": "產(chǎn)品",
      "status": "已處理",
      "datetime": "2014-07-12T21:16:46",
      "replycontent": "好的,只是測試"
    },
    {
      "id": "3a6546f6-49a7-4a17-b679-b3812b12b27e",
      "code": "8147056167227050269",
      "title": "我建議龍頭有多種選配方式",
      "type": "產(chǎn)品",
      "status": "未處理",
      "datetime": "2014-07-12T18:49:08.933",
      "replycontent": ""
    },
    {
      "id": "f735e461-ca72-4b44-8d7b-cd97ac09802f",
      "code": "8147056167227050268",
      "title": "這個(gè)產(chǎn)品不怎么好,不好用",
      "type": "產(chǎn)品",
      "status": "未處理",
      "datetime": "2014-07-12T15:06:19.1",
      "replycontent": ""
    },
    {
      "id": "15926d9d-f469-4921-b01d-4b48ef8bd93d",
      "code": "7141054273018032465",
      "title": "jdjbcn",
      "type": "服務(wù)",
      "status": "未處理",
      "datetime": "2014-05-27T01:03:46.477",
      "replycontent": ""
    },
    {
      "id": "1debf78f-42b3-4037-b71f-34075eed92bc",
      "code": "4141051277003536211",
      "title": "jdjbxn.x",
      "type": "服務(wù)",
      "status": "未處理",
      "datetime": "2014-05-27T00:53:21.18",
      "replycontent": ""
    },
    {
      "id": "27593c52-b327-4557-8106-b9156df53909",
      "code": "1143051276001357050",
      "title": "ghggghh",
      "type": "服務(wù)",
      "status": "未處理",
      "datetime": "2014-05-27T00:35:05.933",
      "replycontent": ""
    },
    {
      "id": "040198fc-b466-46c1-89d8-0514fbde9480",
      "code": "4142053251166372433",
      "title": "你好,你知道啦,我不喜歡白色浴缸",
      "type": "服務(wù)",
      "status": "未處理",
      "datetime": "2014-05-25T16:37:43.853",
      "replycontent": ""
    },
    {
      "id": "16185418-d461-4e98-83c3-824eb7e344d6",
      "code": "4145058213013197148",
      "title": "hdjbchh",
      "type": "服務(wù)",
      "status": "未處理",
      "datetime": "2014-05-21T01:19:14.903",
      "replycontent": ""
    },
    {
      "id": "6c043404-c1db-42e8-adeb-d4880fa7d1b5",
      "code": "0142051185128085372",
      "title": "ghhjdhd",
      "type": "服務(wù)",
      "status": "未處理",
      "datetime": "2014-05-18T12:08:37.997",
      "replycontent": ""
    },
    {
      "id": "2dca1a38-a32b-4955-a99c-2ed7d6de60fa",
      "code": "3146050186122030382",
      "title": "hsibcn",
      "type": "服務(wù)",
      "status": "未處理",
      "datetime": "2014-05-18T12:03:38.913",
      "replycontent": ""
    }
  ]
}

實(shí)現(xiàn)代碼如下:

private string ConvertJsonString(string str)
{
  //格式化json字符串
  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;
  }
}

PS:關(guān)于json操作,這里再為大家推薦幾款比較實(shí)用的json在線工具供大家參考使用:

在線JSON代碼檢驗(yàn)、檢驗(yàn)、美化、格式化工具:
http://tools.jb51.net/code/json

JSON在線格式化工具:
http://tools.jb51.net/code/jsonformat

在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson

json代碼在線格式化/美化/壓縮/編輯/轉(zhuǎn)換工具:
http://tools.jb51.net/code/jsoncodeformat

在線json壓縮/轉(zhuǎn)義工具:
http://tools.jb51.net/code/json_yasuo_trans

更多關(guān)于C#相關(guān)內(nèi)容還可查看本站專題:《C#字符串操作技巧總結(jié)》、《C#常見控件用法教程》、《WinForm控件用法總結(jié)》、《C#程序設(shè)計(jì)之線程使用技巧總結(jié)》、《C#中XML文件操作技巧匯總》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#數(shù)組操作技巧總結(jié)》及《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》

希望本文所述對大家C#程序設(shè)計(jì)有所幫助。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI