Json:JsonConvert.DeserializeObject-无结果无错误
我以前成功地使用Newtonsoft.Json来反序列化Json,但我在这个简单的示例中遇到了一个问题-请参见下文-反序列化方法没有失败,但它没有用预期的值实例化“ServiceResponse”类。调试器显示: ServiceResponse.StatusInfo=null, ServiceResponse.Email=null, ServiceResponse.JobId=0Json:JsonConvert.DeserializeObject-无结果无错误,json,serialization,Json,Serialization,我以前成功地使用Newtonsoft.Json来反序列化Json,但我在这个简单的示例中遇到了一个问题-请参见下文-反序列化方法没有失败,但它没有用预期的值实例化“ServiceResponse”类。调试器显示: ServiceResponse.StatusInfo=null, ServiceResponse.Email=null, ServiceResponse.JobId=0 // Generated by Xamasoft JSON Class Generator // http://ww
// Generated by Xamasoft JSON Class Generator
// http://www.xamasoft.com/json-class-generator
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Example.fmeResponseJsonTypes;
namespace Example.fmeResponseJsonTypes
{
public class StatusInfo
{
[JsonProperty("mode")]
public string Mode { get; set; }
[JsonProperty("status")]
public string Status { get; set; }
}
public class ServiceResponse
{
[JsonProperty("statusInfo")]
public StatusInfo StatusInfo { get; set; }
[JsonProperty("email")]
public string Email { get; set; }
[JsonProperty("jobID")]
public int JobID { get; set; }
}
}
namespace Example
{
public class fmeResponse
{
[JsonProperty("serviceResponse")]
public ServiceResponse ServiceResponse { get; set; }
public void Deserialize()
{
string res = "{\"serviceResponse\": {\"statusInfo\": {\"mode\": \"async\",\"status\": \"success\"},\"email\": \"tor.nielsen@xxx.com\",\"jobID\": 73}}";
ServiceResponse serviceResponse = null;
try
{
serviceResponse = JsonConvert.DeserializeObject<ServiceResponse>(res);
}
catch (Exception e)
{
throw new Exception("Error: Deserialization of [" + res + "] failed! \nDetails: " + e.Message + "\nTrace: " + e.StackTrace);
}
}
}
}
//由Xamasoft JSON类生成器生成
// http://www.xamasoft.com/json-class-generator
使用制度;
使用System.Collections.Generic;
使用Newtonsoft.Json;
使用Newtonsoft.Json.Linq;
使用Example.fmereresponsejsontypes;
namespace Example.fmereresponsejsontypes
{
公共类状态信息
{
[JsonProperty(“模式”)]
公共字符串模式{get;set;}
[JsonProperty(“状态”)]
公共字符串状态{get;set;}
}
公共类服务响应
{
[JsonProperty(“statusInfo”)]
公共状态信息状态信息{get;set;}
[JsonProperty(“电子邮件”)]
公共字符串电子邮件{get;set;}
[JsonProperty(“jobID”)]
public int JobID{get;set;}
}
}
名称空间示例
{
公共类响应
{
[JsonProperty(“serviceResponse”)]
公共服务响应服务响应{get;set;}
public void反序列化()
{
字符串res=“{\'serviceResponse\”:{\'statusInfo\”:{\'mode\”:\'async\”,\'status\“:\'success\”,\'email\“:\”tor。nielsen@xxx.com\“,\“作业ID\”:73}}”;
ServiceResponse ServiceResponse=null;
尝试
{
serviceResponse=JsonConvert.DeserializeObject(res);
}
捕获(例外e)
{
抛出新异常(“错误:反序列化[“+res+”]失败!\n详细信息:“+e.Message+”\n链接:“+e.StackTrace”);
}
}
}
}
试试这个,
请添加JsonProperty,我做得很匆忙
如果您不确定JsonString所需的类,请尝试。有时候真的很有帮助
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Example.fmeResponseJsonTypes;
namespace Example.fmeResponseJsonTypes
{
public class StatusInfo
{
public string mode { get; set; }
public string status { get; set; }
}
public class ServiceResponse
{
public StatusInfo statusInfo { get; set; }
public string email { get; set; }
public int jobID { get; set; }
}
public class RootObject
{
public ServiceResponse serviceResponse { get; set; }
}
}
namespace Example
{
public class fmeResponse
{
public RootObject RootObject { get; set; }
public void Deserialize()
{
string res = "{\"serviceResponse\": {\"statusInfo\": {\"mode\": \"async\",\"status\": \"success\"},\"email\": \"tor.nielsen@xxx.com\",\"jobID\": 73}}";
ServiceResponse serviceResponse = null;
RootObject rootObject = null;
try
{
rootObject = JsonConvert.DeserializeObject<RootObject>(res);
}
catch (Exception e)
{
throw new Exception("Error: Deserialization of [" + res + "] failed! \nDetails: " + e.Message + "\nTrace: " + e.StackTrace);
}
}
}
使用系统;
使用System.Collections.Generic;
使用Newtonsoft.Json;
使用Newtonsoft.Json.Linq;
使用Example.fmereresponsejsontypes;
namespace Example.fmereresponsejsontypes
{
公共类状态信息
{
公共字符串模式{get;set;}
公共字符串状态{get;set;}
}
公共类服务响应
{
公共状态信息状态信息{get;set;}
公共字符串电子邮件{get;set;}
public int jobID{get;set;}
}
公共类根对象
{
公共服务响应服务响应{get;set;}
}
}
名称空间示例
{
公共类响应
{
公共根对象根对象{get;set;}
public void反序列化()
{
字符串res=“{\'serviceResponse\”:{\'statusInfo\”:{\'mode\”:\'async\”,\'status\“:\'success\”,\'email\“:\”tor。nielsen@xxx.com\“,\“作业ID\”:73}}”;
ServiceResponse ServiceResponse=null;
RootObject RootObject=null;
尝试
{
rootObject=JsonConvert.DeserializeObject(res);
}
捕获(例外e)
{
抛出新异常(“错误:反序列化[“+res+”]失败!\n详细信息:“+e.Message+”\n链接:“+e.StackTrace”);
}
}
}