C# Json格式字符串

转自:http://www.cnblogs.com/unintersky/p/3884712.html

将Json字符串转化成格式化表示的方法: 字符串反序列化为对象-->对象再序列化为字符串

使用Newtonsoft.Json提供的API,下载地址:http://www.newtonsoft.com/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": ""

}

]

}

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;
           }
       }
时间: 2024-10-01 07:35:59

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

JSON(二)——JavaScript中js对象与JSON格式字符串的相互转换

首先我们来看一下js中JSON格式的字符串 var JSONStr1 = "{\"name\" : \"张三\"}"; 注意以下的写法不是js中JSON格式的字符串,它只是一个js的字符串. var JSONStr2 = "{'name' : '张三'}"; 我们看JSON语法中对于字符串的定义:是由双引号包围的任意数量Unicode字符的集合,使用反斜线转义.一个字符(character)即一个单独的字符串(characte

json格式字符串处理

public class InternalClass         {             public int MID;             public string Name;             public string Des;             public string Disable;             public string Remark;         }         public class OuterClass         {  

字典和JSON格式字符串相互转换

在iOS开发中,和服务器交互中,经常用到字典和JSON格式字符串相互转换. 1.JSON格式字符串转字典 + (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString { if (jsonString == nil) { return nil; } NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; NSError *err; NSDi

如何将json格式字符串转换为对象

如何将json格式字符串转换为对象: 下面通过实例简单介绍一下如何将json格式的字符串转换为对象. json格式字符串如下: var str="{'webName':'蚂蚁部落','address':'青岛'}"; 上面是一个字符串,下面就将它转换为对象,代码如下: var str="{'webName':'蚂蚁部落','address':'青岛'}"; var obj=eval("("+str+")"); document

iOS:JSON格式字符串转字典,字典转JSON格式字符串

在iOS开发中,和服务器交互中,经常用到字典和JSON格式字符串相互转换. 代码如下: 1.JSON格式字符串转字典 + (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString { if (jsonString == nil) { return nil; } NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; NSError *err

FastJson对于JSON格式字符串、JSON对象及JavaBean之间的相互转换

fastJson对于json格式字符串的解析主要用到了一下三个类: JSON:fastJson的解析器,用于JSON格式字符串与JSON对象及javaBean之间的转换. JSONObject:fastJson提供的json对象. JSONArray:fastJson提供json数组对象. 我们可以把JSONObject当成一个Map<String,Object>来看,只是JSONObject提供了更为丰富便捷的方法,方便我们对于对象属性的操作.我们看一下源码. 同样我们可以把JSONArra

json格式字符串用jquery.parseJSON()出现的问题 Uncaught SyntaxError: Unexpected token &#39; Uncaught SyntaxError: Unexpected number (index)

Unexpected number(index)的错误用的json字符串如 var jsonStr = "{1:'北京note备注信息',2:'上海note备注信息',3:'广东note备注信息',4:'深圳note备注信息4',5:'天津note备注信息5',6:'湖北note备注信息6',7:'重庆note备注信息7',8:'欧盟note备注信息8',9:'加利福尼亚州note备注信息8',10:'加拿大魁北克note信息',11:'美国东北部RGGI协会组织',12:'澳大利亚备注信息',

FastJson学习:JSON格式字符串、JSON对象及JavaBean之间的相互转换

当前台需要传送一系列相似数据到后端时,可以考虑将其组装成json数组对象,然后转化为json形式的字符串传输到后台 例如: nodes = $('#PmPbsSelect_tree').tree('getChecked'); var data=[]; for(var i=0;i<nodes.length;i++){ if(!isParentCheck(nodes[i],nodes)){ data.push({"id":nodes[i].id, "pid":no

获取json格式字符串的简单方法

有的时候需要找一些Json格式的字符串,可以打开任意一个网页进入到调试模式,然后看network相关的访问信息,就可以获取到. 比如: 在记笔记的时候,点击保存后,会发出一些请求,然后有相应的相应,任意找一个request,切换到Response标签, 如: 然后把这个Response的内容考出来就是json格式的.如果要格式化,可以通过在线的格式化工具进行格式化.如: http://tool.oschina.net/codeformat/json 这个网站进行格式化. 格式化的结果如:

java之后台返回json格式字符串,前台接受并转为json文件

作为一个菜鸟,做项目真的好困难呀,这两天被一个问题困了两天,终于解决了,但是也不算太完美.首先,先说一下问题吧,根据后台返回的值,前台接受并作出一个折线图. 最初,在后台根据从数据库中的值,强制拼成了json格式的字符串,利用model返回给前台,前台Ajax  success:function(data)data接收数据,这是应该接收的是字符串,由于折线图中series中的data为非字符串,(这个是后来百度才知道的),就没有成功.然后就想后台直接将数据转换成json,然后返回前台,利用里JS