将 Json List<object> 解析为 C# 中的具体模型
Parsing Json List<object> to a concrete model In C#
目前我有这个 json:
{
"ResponseCode":200,
"data":[
"2016-08-05T15:49:15.157000+00:00",
"SomeString",
1230,
9.025
]
}
我可以将 json 字符串反序列化为
public class Example1
{
public int ResponseCode { get; set; }
public IList<object> data { get; set; }
}
但我想将“数据”反序列化为具体模型而不是
IList<object>
public class Example1
{
public int ResponseCode { get; set; }
public DataModel data { get; set; }
}
public class DataModel
{
public DateTime date { get; set; }
public string SomeString { get; set; }
...
}
我正在使用 System.Text.Json nuget 包。
我应该怎么办?我可以使用 JsonConvert<T>
的自定义实现来解决这个问题吗?
如果您想使用 DataModel class,您需要更新您的 json 值:
{
"ResponseCode":200,
"data": {
"date": "2016-08-05T15:49:15.157000+00:00",
"someString": "SomeString",
"anotherVal": 1230,
"anotherVal2": 9.025
}
}
其中数据是 Example1.data 参数,"date"、"someString"、"anotherVal" 和 "anotherVal2" 是 DataModel 参数
目前我有这个 json:
{
"ResponseCode":200,
"data":[
"2016-08-05T15:49:15.157000+00:00",
"SomeString",
1230,
9.025
]
}
我可以将 json 字符串反序列化为
public class Example1
{
public int ResponseCode { get; set; }
public IList<object> data { get; set; }
}
但我想将“数据”反序列化为具体模型而不是
IList<object>
public class Example1
{
public int ResponseCode { get; set; }
public DataModel data { get; set; }
}
public class DataModel
{
public DateTime date { get; set; }
public string SomeString { get; set; }
...
}
我正在使用 System.Text.Json nuget 包。
我应该怎么办?我可以使用 JsonConvert<T>
的自定义实现来解决这个问题吗?
如果您想使用 DataModel class,您需要更新您的 json 值:
{
"ResponseCode":200,
"data": {
"date": "2016-08-05T15:49:15.157000+00:00",
"someString": "SomeString",
"anotherVal": 1230,
"anotherVal2": 9.025
}
}
其中数据是 Example1.data 参数,"date"、"someString"、"anotherVal" 和 "anotherVal2" 是 DataModel 参数