Newtonsoft.Json解析Json字符串案例:
/// <summary>
/// 上行jsom格式日志记录
/// </summary>
/// <param name="responseJson"></param>
public static void WriteToMoJsomLog(string responseJson)
{
//{"error":"1","remark":"成功","statusbox":[{"mobile":"15510331875","taskid":"123","receivetime":"2015-01-01 00:00:00","errorcode":"dEv"},{"mobile":"13483728958","taskid":"124", "receivetime":"2015-02-01 00:00:00","errorcode":"back"}]}
//{"error":"1","remark":"成功","callbox":[{"mobile":"15510331875","taskid":"","content":"a","receivetime":"0001-01-01 00:00:00", "extno":"123" },{"mobile":"13483728958","taskid":"","content":"b","receivetime":"0001-01-01 00:00:00","extno":"456"}]}
try
{
MoObject obj = JsonDeserialize<MoObject>(responseJson);
string pathConfig = App.GetSetting("TracePath");
if (!string.IsNullOrWhiteSpace(pathConfig))
{
Log4NetTraceListener log = new Log4NetTraceListener(pathConfig + @"\UserInterface");
if (obj.Error == )
{
foreach (MoContent item in obj.Callbox)
{
log.WriteLine("获取上行成功:error :1"+",Remark:"+obj.Remark);
log.WriteLine("获取上行返回信息:" + "mobile:" +item.Mobile+ ",taskid:" + item.TaskId + ",content:" + item.Content + ",receivetime:" + item.ReceiveTime + ",extno:" +item.Extno ); }
}
else
{
log.WriteLine("获取上行Json格式返回错误:error:"+obj.Error+"Remark:"+obj.Remark);
}
}
}
catch (Exception ex)
{ throw new Exception("获取Json格式上行内容写入日志出错:" + ex);
}
未解析建的类型:
/// <summary>
/// 上行状态报告类型
/// </summary>
public class MoObject
{
public int Error { get; set; }
public string Remark { get; set; }
public MoContent[] Callbox { get; set; }
}
/// <summary>
/// 上行和状态报告内容类型
/// </summary>
public class MoContent
{
public string Mobile { get; set; }
public string TaskId { get; set; }
public string Content { get; set; }
public DateTime ReceiveTime { get; set; }
public string Extno { get; set; }
public string ErrorCode { get; set; }
}
解析方法:
public static T JsonDeserialize<T>(string json) where T : class
{
if (string.IsNullOrWhiteSpace(json))
{
throw new ArgumentException("json");
}
return JsonConvert.DeserializeObject<T>(json);
}
Newtonsoft.Json解析Json字符串案例:的更多相关文章
- 【C#】使用Json.NET(newtonsoft)解析Json
最近做的WPF项目中,需要一个C#下的Json实现. 在Json的官网http://www.json.org/中查找,可见C#的Json工具主要有如下: 尝试了排在最前面的fastJSON,反序列化时 ...
- .Net利用Newtonsoft进行解析Json的快捷方法
现在异构系统之间的数据交换多采用Json格式 .Net如何快捷地解析Json和将Object转换成json呢? 1.利用Newtonsoft解析Json字符串 在百度上查找资料,利用Newtonsof ...
- json解析json字符串时候,数组必须对应jsonObjectArray,不能对应JsonObject。否则会解析错误。
json第三方解析json字符串时候,json数组必须对应jsonObjectArray,不能对应JsonObject.->只要是[]开头的都是json数组字符串,就要用jsonArray解析 ...
- Json转model对象,model转json,解析json字符串
GitHub链接: https://github.com/mozhenhau/D3Json D3Json 通过swift的反射特性,把json数据转换为model对象,本类最主要是解决了其他一般jso ...
- 一、JSON解析与字符串化
JSON.stringify() 序列化对象.数组或原始值 语法:JSON.stringify(o,filter,indent) o,要转换成JSON的对象.数组或原始值 filter,指定要序列化的 ...
- c# 使用Newtonsoft.Json解析JSON数组
一.获取JSon中某个项的值 要解析格式: [{"VBELN":"10","POSNR":"10","RET_ ...
- C# Json解析Json = "{\"EX_RETURN\":[{\"MATNR\":\"test\"}] }";
string jtext = "{\"jiangsu\":[{\"wuxi\":\"无锡\"},{\"suzhou\&q ...
- 认识Json解析json生成json
.markdown-body hr::after,.markdown-body::after { clear: both } .loopLine,.messageLine0 { } .markdown ...
- Newtonsoft.Json解析json字符串和写json字符串
写: StringWriter sw = new StringWriter(); JsonWriter writer = new JsonWriter(sw); //如果报错则使用JsonWriter ...
随机推荐
- iOS各种开源类库
KissXml——xml解析库 相关教程:http://www.iteye.com/topic/625849 http://sencho.blog.163.com/blog/static/830562 ...
- Linux压缩打包命令
tar命令 [root@linux ~]# tar [-cxtzjvfpPN] 文件与目录 .... 参数 -c :建立一个压缩文件的参数指令(create 的意思): -x :解开一个压缩文件的参数 ...
- border在IE6设置transparent无效
在ie6下给border设置transparent是无效的,解决办法如下: _border-color:tomato; /*For IE6-*/ _filter:chroma(color=tomato ...
- 一键安装lamp环境 centos
linux centos yum安装LAMP环境 /*************链接**************/http://www.cnblogs.com/suger/p/3832093.html ...
- Java代理(jdk静态代理、动态代理和cglib动态代理)
一.代理是Java常用的设计模式,代理类通过调用被代理类的相关方法,并对相关方法进行增强.加入一些非业务性代码,比如事务.日志.报警发邮件等操作. 二.jdk静态代理 1.业务接口 /** * 业务接 ...
- [JS]递归对象或数组
function recursive(obj) { var output = ''; if (typeof obj === 'object') { for (var key in obj) { var ...
- Python自动化 【第十一篇】:Python进阶-RabbitMQ队列/Memcached/Redis
本节内容: RabbitMQ队列 Memcached Redis 1. RabbitMQ 安装 http://www.rabbitmq.com/install-standalone-mac.htm ...
- BAT的面试经验_摘抄
一.心态 心态很重要! 心态很重要! 心态很重要! 重要的事情说三遍,这一点我觉得是必须放到前面来讲. 找工作之前,有一点你必须清楚,就是找工作是一件看缘分的事情,不是你很牛逼,你就一定能进你想进的公 ...
- docker在ubuntu14.04下的安装笔记
本文主要是参考官网教程进行ubuntu14.04的安装. 下面是我的安装笔记. 笔记原件完整下载: 链接: https://pan.baidu.com/s/1dEPQ8mP 密码: gq2p
- TJI读书笔记16-异常处理
TJI读书笔记16-异常处理 概念 基本异常情形 异常的捕获 自定义异常 异常说明 捕获所有异常 栈轨迹 重新抛出异常 Java标准异常 使用finally 异常的限制 构造器 异常的匹配 其他乱七八 ...