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 ...
随机推荐
- IE10中session失效取不到值的问题
在eworkflow工作流,ebiao报表,eform自定义表单产品升级到IE10的时候,系统登录后,总是会取不到session中的值. for j2ee版本和for dotnet版本都一样取不到值. ...
- GCD、dispatch函数介绍
iOS多线程的方法有3种: NSThread NSOperation GCD(Grand Central Dispatch) 其中,由苹果所倡导的为多核的并行运算提出的解决方案:GCD能够访问线程池, ...
- jquery文件上传控件 Uploadify
(转自 http://www.cnblogs.com/mofish/archive/2012/11/30/2796698.html) 基于jquery的文件上传控件,支持ajax无刷新上传,多个文件同 ...
- django修改admin密码
python manage.py shell 然后获取你的用户名,并且重设密码:from django.contrib.auth.models import User user = User.obje ...
- linux top命令中各cpu占用率含义
linux top命令中各cpu占用率含义 [尊重原创文章摘自:http://www.iteye.com/topic/1137848]0.3% us 用户空间占用CPU百分比 1.0% sy 内核空间 ...
- a different object with the same identifier value was already associated with the session:错误;
当出现a different object with the same identifier value was already associated with thesession时,一般是因为在h ...
- Hive的安装
Hive的安装 第一步:解压并安装:第二步:配置 1)root用户下,解压后,改名为hive,并将hive文件夹赋给hadoop用户 tar -zxvf hive-0.9.0.tar.gz -C ...
- C#绘制传感器代码
//以下代码添加到任一窗口下即可 private int 旋转角度 = 0; private int 边长 = 10; protected override ...
- nmon工具的安装及简单使用
1.工具的安装 下载rpm包安装即可http://mirror.ghettoforge.org/distributions/gf/el/6/gf/x86_64/nmon-14i-1.gf.el6.x8 ...
- My97DatePicker 日期控制,开始时间不能>结束时间,结束时间不能<开始时间
<li>日期: <input type="text" style="margin-top: 5px;" value="${begin ...