C#格式化JSON字符串

很多时候我们需要将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字符串

时间: 2024-11-09 01:53:34

C#格式化JSON字符串的相关文章

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

json.Append(String.Format("{\"total\":{0},\"row\":{1}}", lineCount, strJSON));直接会报错 字符串中包含{或者},则需要用{{ 来代替字符 {,用}} 代替 }如:json.Append(String.Format("{{\"total\":{0},\"row\":{1}}}", lineCount, strJS

MAC上安装并破解最新SublimeText3103版本,安装PrettyJSON离线格式化json字符串

开发中少不了将JSON字符串格式化,方便查看.之前都是在线转,最近知道ST安装个插件就可以离线格式化JSON就又倒腾起来了.还真遇到点麻烦,故此记录. 1,官网下载 Sublime Text Build 3103.dmg,双击安装: 2,输入注册码: -– BEGIN LICENSE -–Ryan ClarkSingle User LicenseEA7E-8124792158A7DE B690A7A3 8EC04710 006A5EEB34E77CA3 9C82C81F 0DB6371B 797

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

JSON格式化工具,简易实现.作为技术宅,直接上代码,供大家使用.前提:一定要引入jquery哦. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <style> #btn{width:100px;height:30px;line-height:30px;text-align:center;display:block;margin

Notepad++格式化JSON字符串

窗口 -> 插件 -> Plugin Manager -> Show Plugin Manager,在弹出层中找到"JSON Viewer"选项,将踏勾选上,然后点击下面的"Install"选项.等待安装完毕.安装完成后,选中你需要格式化的JSON内容,然后点击上面的"插件"选项,这是我们可以看到之前没有的"JSON Viewer"选项,然后选中----"Format JSON"选项.

格式化JSON字符串

提出需求 异步调用获取JSON数据时非常不直观,每次都需要格式化一次,才能直观的看到数据集合的结构,现在需要实现输出带缩进的格式. 实现效果 在浏览器的查看源文件中已经实现格式化,如果是页面使用,可以直接赋值给textarea同样能以格式化后的效果显示. 实现代码 public static String Format(string text) { string result = string.Empty; try { JsonSerializer serializer = new JsonSe

php json 初始化函数(格式化json字符串为php json_decode 标准的字符串)

$json="[{ 'i':100000, 'u':-1,n: '中国'},{i:100001,u:-1,n:'阿尔巴尼亚'},{i:100002,u:-1,n:'阿尔及利亚',}]"; $json = preg_replace('/([{,:])(\s*)\'(.*?)\'/','$1"$3"',$json);//key.value的单引号变双引号 $json = preg_replace('/([{,]+)(\s*)([^"\']+?)\s*:/','

格式化JSON数据字符串

创建项目 创建maven项目,引入解析JSON所需jar包.pom.xml配置如下: <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <!--指定jdk版本 --> <classifier>jdk15</classifier>

springmvc json字符串转化成json对象

问题出现在 :页面数据列表的展示出现 [object HTMLInputElement] 找到问题的所在原因后又三种解决方案 一:格式化json字符串为json对象字符串 success:function(data){ data=eval('('+data+')'); //在返回的数据里加上这就代码, alert(data);} 二:指定数据类型为json $.ajax({ type:'post', data:{pageSize:15,currentPage:1}, url:'<%=ctxPat

HTML代码,CSS代码,JSON字符串在线格式化,美化工具

HTML代码,CSS代码,JSON字符串在线格式化,美化工具 http://www.phpthinking.com/format.htm