1. /// <summary>
  2. /// 上行jsom格式日志记录
  3. /// </summary>
  4. /// <param name="responseJson"></param>
  5. public static void WriteToMoJsomLog(string responseJson)
  6. {
  7. //{"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"}]}
  8. //{"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"}]}
  9. try
  10. {
  11. MoObject obj = JsonDeserialize<MoObject>(responseJson);
  12. string pathConfig = App.GetSetting("TracePath");
  13. if (!string.IsNullOrWhiteSpace(pathConfig))
  14. {
  15. Log4NetTraceListener log = new Log4NetTraceListener(pathConfig + @"\UserInterface");
  16. if (obj.Error == )
  17. {
  18. foreach (MoContent item in obj.Callbox)
  19. {
  20. log.WriteLine("获取上行成功:error :1"+",Remark:"+obj.Remark);
  21. log.WriteLine("获取上行返回信息:" + "mobile:" +item.Mobile+ ",taskid:" + item.TaskId + ",content:" + item.Content + ",receivetime:" + item.ReceiveTime + ",extno:" +item.Extno );
  22.  
  23. }
  24. }
  25. else
  26. {
  27. log.WriteLine("获取上行Json格式返回错误:error:"+obj.Error+"Remark:"+obj.Remark);
  28. }
  29. }
  30. }
  31. catch (Exception ex)
  32. {
  33.  
  34. throw new Exception("获取Json格式上行内容写入日志出错:" + ex);
  35. }

未解析建的类型:

  1. /// <summary>
  2. /// 上行状态报告类型
  3. /// </summary>
  4. public class MoObject
  5. {
  6. public int Error { get; set; }
  7. public string Remark { get; set; }
  8. public MoContent[] Callbox { get; set; }
  9. }
  10. /// <summary>
  11. /// 上行和状态报告内容类型
  12. /// </summary>
  13. public class MoContent
  14. {
  15. public string Mobile { get; set; }
  16. public string TaskId { get; set; }
  17. public string Content { get; set; }
  18. public DateTime ReceiveTime { get; set; }
  19. public string Extno { get; set; }
  20. public string ErrorCode { get; set; }
  21. }

解析方法:

  1. public static T JsonDeserialize<T>(string json) where T : class
  2. {
  3. if (string.IsNullOrWhiteSpace(json))
  4. {
  5. throw new ArgumentException("json");
  6. }
  7. return JsonConvert.DeserializeObject<T>(json);
  8. }

Newtonsoft.Json解析Json字符串案例:的更多相关文章

  1. 【C#】使用Json.NET(newtonsoft)解析Json

    最近做的WPF项目中,需要一个C#下的Json实现. 在Json的官网http://www.json.org/中查找,可见C#的Json工具主要有如下: 尝试了排在最前面的fastJSON,反序列化时 ...

  2. .Net利用Newtonsoft进行解析Json的快捷方法

    现在异构系统之间的数据交换多采用Json格式 .Net如何快捷地解析Json和将Object转换成json呢? 1.利用Newtonsoft解析Json字符串 在百度上查找资料,利用Newtonsof ...

  3. json解析json字符串时候,数组必须对应jsonObjectArray,不能对应JsonObject。否则会解析错误。

    json第三方解析json字符串时候,json数组必须对应jsonObjectArray,不能对应JsonObject.->只要是[]开头的都是json数组字符串,就要用jsonArray解析 ...

  4. Json转model对象,model转json,解析json字符串

    GitHub链接: https://github.com/mozhenhau/D3Json D3Json 通过swift的反射特性,把json数据转换为model对象,本类最主要是解决了其他一般jso ...

  5. 一、JSON解析与字符串化

    JSON.stringify() 序列化对象.数组或原始值 语法:JSON.stringify(o,filter,indent) o,要转换成JSON的对象.数组或原始值 filter,指定要序列化的 ...

  6. c# 使用Newtonsoft.Json解析JSON数组

    一.获取JSon中某个项的值 要解析格式: [{"VBELN":"10","POSNR":"10","RET_ ...

  7. C# Json解析Json = "{\"EX_RETURN\":[{\"MATNR\":\"test\"}] }";

    string jtext = "{\"jiangsu\":[{\"wuxi\":\"无锡\"},{\"suzhou\&q ...

  8. 认识Json解析json生成json

    .markdown-body hr::after,.markdown-body::after { clear: both } .loopLine,.messageLine0 { } .markdown ...

  9. Newtonsoft.Json解析json字符串和写json字符串

    写: StringWriter sw = new StringWriter(); JsonWriter writer = new JsonWriter(sw); //如果报错则使用JsonWriter ...

随机推荐

  1. C# 访问https 未能创建 SSL/TLS 安全通道

    C# 访问https请求被中止: 未能创建 SSL/TLS 安全通道(Could not create SSL/TLS secure channel) 一般GetResponse可以直接访问https ...

  2. zookeeper分布式部署-mac先测试

    由于平台马上要引入zookeeper+dubbo,为了解决zookeeper单个实例运行的风险,需要做个集群. 1,先说配置:zoo.cfg十分简单,分两种情况: 一种是在一台机器采用不同的端口配置多 ...

  3. C# CRC-CCITT (Kermit)校验方法

    using System; public enum Crc16Mode : ushort { Standard = 0xA001, CcittKermit = 0x8408 } public clas ...

  4. javascript-with()方法

    1)简要说明         with 语句可以方便地用来引用某个特定对象中已有的属性,但是不能用来给对象添加属性.要给对象创建新的属性,必须明确地引用该对象. 2)语法格式  with(object ...

  5. 网络转载——java接口的概念

    为什么会出现接口? 接口的出现是为了扩展java中的类继承的单调性.这样使得功能更加丰富. 接口关键字? 定义接口interface,实现一个接口  implements 什么接口呢? 接口是一种特殊 ...

  6. scrollbar_test

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  7. gdb汇编调试

    GDB调试汇编堆栈分析 代码: sudo apt-get install libc6-dev-i386命令安装所需库 进入之后先在main函数处设置一个断点,再run一下,使用disassemble指 ...

  8. css定位和浮动

    1.css中一切元素皆为框.div.p.h1等为块框:span.strong等为行内框,(在文本中每一行会被自动默认为行框,行框和行内框是不一样的概念).通过display可以改变框的类型,行内框通过 ...

  9. BufferedReader 中的 readLine()

    BufferedReader中的readLine()方法,API解释如下: Reads a line of text. A line is considered to be terminated by ...

  10. index()、e.target.value、on()与快捷处理的区别、

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...