网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

C#中将Json字符串转化成格式化表示的方法是什么

这篇文章主要介绍了C#中将Json字符串转化成格式化表示的方法是什么,具有一定借鉴价值,需要的朋友可以参考下。希望大家阅读完这篇文章后大有收获。下面让小编带着大家一起了解一下。

创新互联于2013年创立,先为晋州等服务建站,晋州等地企业,进行企业商务咨询服务。为晋州企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

将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字符串转化成格式化表示的方法是什么内容对大家有帮助,同时也希望大家多多支持创新互联,关注创新互联行业资讯频道,遇到问题就找创新互联,详细的解决方法等着你来学习!


分享标题:C#中将Json字符串转化成格式化表示的方法是什么
链接地址:http://bjjierui.cn/article/gggpcd.html

其他资讯