RestSharp 反序列化 JSON 字典
RestSharp Deserialize JSON Dictionary
我在使用 RestSharp 反序列化来自 REST 服务的 JSON 响应时遇到了一些问题,但我怀疑这会发生在 Newtonsoft 或其他库中,这是一个序列化问题而不是特定于库的问题.
响应是部分 dictionary/collection,部分响应代码,但 dictionary/collection 元素未显示为数组,而是具有数字 属性 名称的项目。
{ "StatusCode": 1, "1": { forename: "Test", surname: "Subject", addressLine1: "1 The Street" }, "2": { ... }, "3": { ... } ... }
我正在尝试将其反序列化为 POCO,但我不确定如何反序列化那些编号的项目。以前有没有人这样做过,或者知道我该怎么做?我对 POCO 并不看重,任何有用的东西都很好。
public class ServiceResponse
{
public int StatusCode { get; set; }
public Dictionary<int, ServiceResponseItem> Items { get; set; }
}
public class ServiceResponseItem
{
public string Forename { get; set; }
public string Surname { get; set; }
public string AddressLine1 { get; set; }
}
我是通过下面的代码实现的:
dynamic res = JsonConvert.DeserializeObject(
"{ \"StatusCode\": 1, \"1\": { \"forename\": \"Test\", \"surname\": \"Subject\", \"addressLine1\": \"1 The Street\" }}");
IDictionary<string, JToken> datas = res;
foreach (var dt in datas.Skip(1))
{
Info newInfo = JsonConvert.DeserializeObject<Info>(dt.Value.ToString());
}
public class Whosebug
{
public int StatusCode { get; set; }
public Info Info { get; set; }
}
public class Info
{
public string forename { get; set; }
public string surname { get; set; }
public string addressLine1 { get; set; }
}
最终使用以下方法解决了这个问题(返回的类型是 JObject,这就是为什么它不会根据 @FaizanRabbani 的回答转换为 IDictionary。
public Whosebug Parse(string json)
{
Whosebug response = new Whosebug();
response.Items = new List<Info>();
dynamic res = JsonConvert.DeserializeObject(json);
response.StatusCode = res.StatusCode;
foreach (JProperty item in res)
{
if (item.Name != "StatusCode")
{
var infoItem = JsonConvert.DeserializeObject<Info>(item.Value.ToString());
response.Items.Add(infoItem);
}
}
return response;
}
我在使用 RestSharp 反序列化来自 REST 服务的 JSON 响应时遇到了一些问题,但我怀疑这会发生在 Newtonsoft 或其他库中,这是一个序列化问题而不是特定于库的问题.
响应是部分 dictionary/collection,部分响应代码,但 dictionary/collection 元素未显示为数组,而是具有数字 属性 名称的项目。
{ "StatusCode": 1, "1": { forename: "Test", surname: "Subject", addressLine1: "1 The Street" }, "2": { ... }, "3": { ... } ... }
我正在尝试将其反序列化为 POCO,但我不确定如何反序列化那些编号的项目。以前有没有人这样做过,或者知道我该怎么做?我对 POCO 并不看重,任何有用的东西都很好。
public class ServiceResponse
{
public int StatusCode { get; set; }
public Dictionary<int, ServiceResponseItem> Items { get; set; }
}
public class ServiceResponseItem
{
public string Forename { get; set; }
public string Surname { get; set; }
public string AddressLine1 { get; set; }
}
我是通过下面的代码实现的:
dynamic res = JsonConvert.DeserializeObject(
"{ \"StatusCode\": 1, \"1\": { \"forename\": \"Test\", \"surname\": \"Subject\", \"addressLine1\": \"1 The Street\" }}");
IDictionary<string, JToken> datas = res;
foreach (var dt in datas.Skip(1))
{
Info newInfo = JsonConvert.DeserializeObject<Info>(dt.Value.ToString());
}
public class Whosebug
{
public int StatusCode { get; set; }
public Info Info { get; set; }
}
public class Info
{
public string forename { get; set; }
public string surname { get; set; }
public string addressLine1 { get; set; }
}
最终使用以下方法解决了这个问题(返回的类型是 JObject,这就是为什么它不会根据 @FaizanRabbani 的回答转换为 IDictionary。
public Whosebug Parse(string json)
{
Whosebug response = new Whosebug();
response.Items = new List<Info>();
dynamic res = JsonConvert.DeserializeObject(json);
response.StatusCode = res.StatusCode;
foreach (JProperty item in res)
{
if (item.Name != "StatusCode")
{
var infoItem = JsonConvert.DeserializeObject<Info>(item.Value.ToString());
response.Items.Add(infoItem);
}
}
return response;
}