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 ...
随机推荐
- C# 访问https 未能创建 SSL/TLS 安全通道
C# 访问https请求被中止: 未能创建 SSL/TLS 安全通道(Could not create SSL/TLS secure channel) 一般GetResponse可以直接访问https ...
- zookeeper分布式部署-mac先测试
由于平台马上要引入zookeeper+dubbo,为了解决zookeeper单个实例运行的风险,需要做个集群. 1,先说配置:zoo.cfg十分简单,分两种情况: 一种是在一台机器采用不同的端口配置多 ...
- C# CRC-CCITT (Kermit)校验方法
using System; public enum Crc16Mode : ushort { Standard = 0xA001, CcittKermit = 0x8408 } public clas ...
- javascript-with()方法
1)简要说明 with 语句可以方便地用来引用某个特定对象中已有的属性,但是不能用来给对象添加属性.要给对象创建新的属性,必须明确地引用该对象. 2)语法格式 with(object ...
- 网络转载——java接口的概念
为什么会出现接口? 接口的出现是为了扩展java中的类继承的单调性.这样使得功能更加丰富. 接口关键字? 定义接口interface,实现一个接口 implements 什么接口呢? 接口是一种特殊 ...
- scrollbar_test
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- gdb汇编调试
GDB调试汇编堆栈分析 代码: sudo apt-get install libc6-dev-i386命令安装所需库 进入之后先在main函数处设置一个断点,再run一下,使用disassemble指 ...
- css定位和浮动
1.css中一切元素皆为框.div.p.h1等为块框:span.strong等为行内框,(在文本中每一行会被自动默认为行框,行框和行内框是不一样的概念).通过display可以改变框的类型,行内框通过 ...
- BufferedReader 中的 readLine()
BufferedReader中的readLine()方法,API解释如下: Reads a line of text. A line is considered to be terminated by ...
- index()、e.target.value、on()与快捷处理的区别、
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...