很多时候我们需要将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": ""
        }
    ]
}

 这样就容易多了.代码没做错误处理,读者可自行更改

需要导入Newtonsoft.Json,这里使用的版本是4.5
 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字符串的更多相关文章

  1. C#后台格式化JSON字符串显示

    很多时候我们从服务器上获取的JSON字符串是没有格式化的,如下: {"BusinessId":null,"Code":200,"Data": ...

  2. 【Golang】格式化JSON字符串,方便查看

    分别介绍golang及Python格式化接口返回JSON数据的方法,及Python json.dumps方法出现NameError: name 'true' is not defined原因解析及解决 ...

  3. js格式化json字符串和json对象

    1,格式化json对象 var json = {"@odata.context":"$metadata#AddTableOne_466281s","v ...

  4. 【Json】C#格式化JSON字符串

    很多时候我们需要将json字符串以 {     "status": 1,     "sum": 9 }这种方式显示,而从服务端取回来的时候往往是这样 {&quo ...

  5. (转)淘淘商城系列——使用JsonView来格式化json字符串

    http://blog.csdn.net/yerenyuan_pku/article/details/72846025 有时从服务端返回的json字符串往往晦涩难懂,就像下面这样,一行显示出来,让人非 ...

  6. js 格式化 json 字符串

    1.JSON.stringify的三个参数 var json = {"@odata.context":"$metadata#AddTableOne_466281s&quo ...

  7. JS 前端格式化JSON字符串工具

    JSON格式化工具,简易实现.作为技术宅,直接上代码,供大家使用.前提:一定要引入jquery哦. <!DOCTYPE html> <html lang="en" ...

  8. php格式化json字符串

    header('content-type:application/json;charset=utf8'); $arr = array( 'status' => true, 'errMsg' =& ...

  9. C# String.Format格式化json字符串中包含"{" "}"报错问题

    json.Append(String.Format("{\"total\":{0},\"row\":{1}}", lineCount, st ...

随机推荐

  1. CentOS 5.x版本升级Mysql

    #-----------------------------CentOS 5.x版本升级Mysql ------------------#! /bin/sh #1.关闭selinuxcp -rp /e ...

  2. 2014年第五届蓝桥杯试题C/C++程序设计B组——李白打酒

    题目描述: 标题:李白打酒 话说大诗人李白,一生好饮.幸好他从不开车. 一天,他提着酒壶,从家里出来,酒壶中有酒2斗.他边走边唱: 无事街上走,提壶去打酒. 逢店加一倍,遇花喝一斗. 这一路上,他一共 ...

  3. (转载)前端构建工具gulp使用

    前端构建工具gulp使用 前端自动化流程工具,用来合并文件,压缩等. Gulp官网 http://gulpjs.com/ Gulp中文网 http://www.gulpjs.com.cn/ Gulp中 ...

  4. (转)Android属性设置android:noHistory="true"

    设置 android:noHistory="true"后,该Activity在statck中不留历史痕迹.默认的值是false. 举例说明,假设有三个Activity分别是:A,B ...

  5. HDFS文件读写流程

    一.HDFS HDFS全称是Hadoop Distributed System.HDFS是为以流的方式存取大文件而设计的.适用于几百MB,GB以及TB,并写一次读多次的场合.而对于低延时数据访问.大量 ...

  6. SQL Server中查询结果拼接遇到的小问题

    前天的项目,刚接手,对于模块还不是很熟悉,其中有一个模块,涉及到4个表,其中主要的表就有两个,只要把这个弄清楚了就一切回归于“太平”了. 模块要求:把两个表的内容查询出来,结果连接在一起.大师说完,感 ...

  7. How to get the underlying SSRS Report Query, reset query , add your own ranges and execute report [AX2012]

    Below is the small code snippet to get the underlying query of the SSRS report, reset query, prompt ...

  8. 实战MySQL集群,试用CentOS 6下的MariaDB-Galera集成版

    说起mysql的集群估计很多人会首先想起mysql自带的replication或者mysql-mmm.mysql-mmm其实也是基于mysql自带的replication的,不过封装的更好用一些,但是 ...

  9. open/fopen read/fread write/fwrite区别

    fopen /open区别 UNIX环境下的C 对二进制流文件的读写有两套班子:1) fopen,fread,fwrite ; 2) open, read, write这里简单的介绍一下他们的区别.1 ...

  10. 算法系列8《Base64》

    Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,Base64编码可用于在HTTP环境下传递较长的标识信息.在其他应用程序中,也常常需要把二进制数据编码为适合放在URL(包括隐藏表单 ...