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


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

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;

           }         

       }

原文地址:https://www.cnblogs.com/wlzhang/p/10917791.html

时间: 2024-10-12 21:09:57

json 格式化输出的相关文章

懒人福利:Xcode插件将JSON格式化输出为模型的属性->ESJsonFormat-Xcode

这是一个直接将json数据转换为模型数据的插件,只需要在控制台输入json数据,就可以在模型文件的.h文件中生成对应的模型数据 对于模型套模型的数据也做了处理,比较方便. 有需要的人可以尝试一下,但不知道在xcode8中是否支持 项目主页

【Linux】Linux JSON 格式化输出

1.使用jq命令 $ echo '{"uid":100120,"token":"1fa9fb8004b04f66b7da57393641eddc"}' | jq . 2.使用python -mjson.tool $ echo '{"msg": "ok", "data": {"list": [{"website": "letv",

Log格式化输出JSON内容

以前常做的一件事就是把返回的json复制到格式化工具里看内容,Json格式化方法那么多直接格式化LOG不是很省事. 很早前写的简单LOG类(Log.java) import android.text.TextUtils; /** * 日志信息 * * @author Harlan Song * @createDate 2013-2-8 */ public class Log { /** * 日志开关 */ private static boolean isDebug = true; privat

校验字符串是否是JSON格式,将不规则展示的json格式的字符串进行规则展示(json格式化)

版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] [html] view plain copy var str = {"code": "","svcname": "","version": "","component": "","category": "","requestMs

iOS插件详细之---ESJsonFormat-Xcode----将JSON格式化输出为模型的属性

Xcode插件将JSON格式化输出为模型的属性:ESJsonFormat-Xcode 一,将 JSON 格式化输出为模型的属性 写在之前的注意 JSON中的key对应的value为Null的话会格式化成NSString类型 格式化之前光标放在你需要添加属性的地方 RootClass需要自己手动创建,插件只负责RootClass里面的属性生成 生成的MJExtension 2.0框架中objectClassInArray方法(类方法) 怎么安装: 方式1:下载-Xcode打开-Command+B-

格式化输出Json对象

1.调用方式: alert(JsonUti.convertToString(jsonObj)); //jsonObj为json对象. 2.格式化输出Json对象方法定义: var JsonUti = { //定义换行符 n: "\n", //定义制表符 t: "\t", //转换String convertToString: function(obj) { return JsonUti.__writeObj(obj, 1); }, //写对象 __writeObj:

用Java对JSON字符串进行格式化输出

工作中进行http相关接口测试时经常会收到返回的json数据,因为没有格式化输入到控制台后看起来很不直观. 早上写了一个小工具类,对这JSON串进行格式化输出,代替System.out.print package MyTest; /** * 格式化输入工具类 * @author lizhgb * @date 2015-10-14 * */ public final class FormatUtil { /** * 打印输入到控制台 * @param jsonStr * @author lizhg

Node读取和写入json,格式化输出json

今天是在深信服实习的第5天,接到任务要做产品的外语版,Web端的实习生目前只有我一个,前端和PHP的都要我来做... 现在需要一个写一个脚本,处理两个json文件,把Bjson文件在A中没有的中文词条都要找出来写入另外一个json,实际需求要复杂得多,先写个初步demo: var fs = require('fs'), path = require('path'); let ans = {}; //求一方的差集(不是严格意义上的差集,只是一边的) function diff(a, b) { va

Java - 格式化输出JSON字符串的两种方式

目录 1 使用阿里的fastjson 1.1 项目的pom.xml依赖 1.2 Java示例代码 2 使用谷歌的gson 2.1 项目的pom.xml依赖 2.2 Java示例代码 1 使用阿里的fastjson 1.1 项目的pom.xml依赖 <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.58&l