C#中如何格式化json字串的方法分析

黄舟
發布: 2017-06-18 10:23:31
原創
2670 人瀏覽過

這篇文章主要介紹了C#格式化json字串的方法,結合實例形式分析了C#針對json字串格式化的原理、步驟與具體實現技巧,需要的朋友可以參考下

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

將Json字串轉換成格式化表示的方法: 字串反序列化為物件-->物件再序列化為字串

使用Newtonsoft.Json提供的API,

很多時候我們需要將json字串以


{
  "status": 1,
  "sum": 9
}
登入後複製

這種方式顯示,而從服務端取回的時候往往是這樣


{"status": 1, "sum": 9}
登入後複製

什麼?無所謂?

如果資料很龐大的時候,例如這樣

複製程式碼 程式碼如下:

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

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

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

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

實作程式碼如下:

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;
  }
}
登入後複製

以上是C#中如何格式化json字串的方法分析的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板