反序列化json的正确C#结构是什么?
我从http获取json,并尝试将其反序列化为C#对象,但它一直返回为null,因此我猜我的数据结构已关闭。这是我的密码:反序列化json的正确C#结构是什么?,c#,json,serialization,C#,Json,Serialization,我从http获取json,并尝试将其反序列化为C#对象,但它一直返回为null,因此我猜我的数据结构已关闭。这是我的密码: results = httpClient.GetStringAsync(url).Result; var restResponse = new RestSharp.RestResponse(); restResponse.Content = results; var deserializer = new JsonDeserializer()
results = httpClient.GetStringAsync(url).Result;
var restResponse = new RestSharp.RestResponse();
restResponse.Content = results;
var deserializer = new JsonDeserializer();
var page = _deserializer.Deserialize<Tree>(restResponse);
下面是我的C#对象:
公共类树
{
公共结果页{get;set;}
}
公开课成绩
{
public ResultDetails results{get;set;}
}
公共类结果详细信息
{
公共列表页{get;set;}
}
公共类页面信息
{
公共长id{get;set;}
公共字符串类型{get;set;}
公共字符串状态{get;set;}
公共字符串标题{get;set;}
}
有人能就这里没有“排队”的地方提出建议吗?这将起作用:
public class Tree
{
public Page page { get; set; }
}
public class Page
{
public List<Result> results { get; set; }
public int start { get; set; }
public int limit { get; set; }
public int size { get; set; }
}
public class Result
{
public string id { get; set; }
public string type { get; set; }
public string status { get; set; }
public string title { get; set; }
}
公共类树
{
公共页页面{get;set;}
}
公共类页面
{
公共列表结果{get;set;}
public int start{get;set;}
公共整数限制{get;set;}
公共整数大小{get;set;}
}
公开课成绩
{
公共字符串id{get;set;}
公共字符串类型{get;set;}
公共字符串状态{get;set;}
公共字符串标题{get;set;}
}
results
是JSON中的一个数组,但是您将它定义为一个对象(ResultDetails
)为什么不使用Visual studio直接创建类结构呢..这将为您提供与JSON匹配的类结构
您可以在此处检查如何生成:
复制json>>visual studio编辑菜单>粘贴特殊>>将json粘贴为类
这可能对你有好处
public class Rootobject
{
[JsonProperty("page")]
public Page page { get; set; }
}
public class Page
{
[JsonProperty("results")]
public Result[] results { get; set; }
[JsonProperty("start")]
public int start { get; set; }
[JsonProperty("limit")]
public int limit { get; set; }
[JsonProperty("size")]
public int size { get; set; }
}
public class Result
{
[JsonProperty("id")]
public string id { get; set; }
[JsonProperty("type")]
public string type { get; set; }
[JsonProperty("status")]
public string status { get; set; }
[JsonProperty("title")]
public string title { get; set; }
}
而实施应该是
results = httpClient.GetStringAsync(url).Result;
var restResponse = new RestSharp.RestResponse();
restResponse.Content = results;
var deserializer = new JsonDeserializer();
var page = _deserializer.Deserialize<Rootobject>(restResponse);
results=httpClient.GetStringAsync(url).Result;
var restResponse=new RestSharp.restResponse();
resresponse.Content=结果;
var反序列化器=新的JsonDeserializer();
var page=_反序列化程序。反序列化(重新响应);
我从来都不知道这件事。太棒了
public class Rootobject
{
[JsonProperty("page")]
public Page page { get; set; }
}
public class Page
{
[JsonProperty("results")]
public Result[] results { get; set; }
[JsonProperty("start")]
public int start { get; set; }
[JsonProperty("limit")]
public int limit { get; set; }
[JsonProperty("size")]
public int size { get; set; }
}
public class Result
{
[JsonProperty("id")]
public string id { get; set; }
[JsonProperty("type")]
public string type { get; set; }
[JsonProperty("status")]
public string status { get; set; }
[JsonProperty("title")]
public string title { get; set; }
}
results = httpClient.GetStringAsync(url).Result;
var restResponse = new RestSharp.RestResponse();
restResponse.Content = results;
var deserializer = new JsonDeserializer();
var page = _deserializer.Deserialize<Rootobject>(restResponse);