[英]Deserialize JSON string in to multiple C# objects
I have a JSON string in below format for which I want to deserialize it into C# List. 我有以下格式的JSON字符串,想要将其反序列化为C#List。 But the record number "1","2","3" ( it can be upto 1,2,3...n depends on the json response each time ) in JSON restricting me to deserialize it into C# object using Newtonsoft.Json
但是JSON中的记录号“ 1”,“ 2”,“ 3”( 每次取决于JSON响应,最多可以达到1,2,3 ... n ),这限制了我使用Newtonsoft将其反序列化为C#对象。杰森
{
"1":{
"UID":"1",
"LICENCENO":"licenseno",
"NAME":"ABC"
},
"2":{
"UID":"2",
"LICENCENO":"licenseno",
"NAME":"PQR"
},
"3":{
"UID":"3",
"LICENCENO":"licenseno",
"NAME":"XYZ"
}
}
I am using below code for deserialization 我正在使用以下代码进行反序列化
var result = Newtonsoft.Json.JsonConvert.DeserializeObject<List<DriverMaster>>(json);
I have DriverMaster class created as- 我将DriverMaster类创建为-
public class DriverMaster
{
public string UID { get; set; }
public string LICENCENO { get; set; }
public string NAME { get; set; }
}
Deserialization line giving unhandled exception, I know I am doing it in wrong way, because DriverMaster json object cannot be extracted into c# directly without doing something to record number 1,2,3...n in c#. 反序列化行给出了未处理的异常,我知道我这样做的方式是错误的,因为如果不执行某些操作以在c#中记录数字1,2,3 ... n ,就无法将DriverMaster json对象直接提取到c#中。 Can anyone please help me to sort it out?
谁能帮我解决一下吗? Thanks in advance.
提前致谢。
You were close: 您接近:
var result = JsonConvert.DeserializeObject<Dictionary<string, DriverMaster>>(json)
.Select(x => x.Value)
.ToList();
Solution.
解。
Change your code to use...更改您的代码以使用...
var result = JsonConvert.DeserializeObject<Dictionary<int, DriverMaster>>(json);
Explaination 讲解
The type is not the same... The List<DriverMaster>
type will convert to JSON like so... 类型不同...
List<DriverMaster>
类型将像这样转换为JSON ...
{
"1":
{
"DriverMaster": {
"UID":"1",
"LICENCENO":"licenseno",
"NAME":"ABC"
}
}
}
This doesn't match what you showed in your question... 这与您在问题中显示的内容不符...
The type that you are looking for is actually Dictionary<int, DriverMaster>
, which is a key/value pair which will output a JSON string like so 您要查找的类型实际上是
Dictionary<int, DriverMaster>
,它是一个键/值对,将像这样输出JSON字符串
{
"1": { ... },
"2": { ... },
"3": { ... }
}
In order to fix that, you need to use the Dictionary<int, DriverMaster>
type instead. 为了解决此问题,您需要改用
Dictionary<int, DriverMaster>
类型。
You need to use 您需要使用
public class DriverMaster
{
public string UID { get; set; }
public string LICENCENO { get; set; }
public string NAME { get; set; }
}
public class Root
{
[JsonExtensionData]
public IDictionary<string,JToken> Data {get;set;}
}
and 和
var result = Newtonsoft.Json.JsonConvert.DeserializeObject<Root>(json);
If you want to have result as List, you can parse the result as. 如果要将结果作为List列出,则可以将结果解析为。
var list = new List<DriverMaster>();
foreach(KeyValuePair<string, JToken> token in result.Data)
{
list.Add(token.Value.ToObject<DriverMaster>());
}
That would give you the desired result as 那会给你期望的结果
1 licenseno ABC
2 licenseno PQR
3 licenseno XYZ
For these types of things I like to use the often overlooked feature of JToken.SelectTokens
. 对于这些类型的事情,我喜欢使用
JToken.SelectTokens
经常被忽略的功能。 This function allows you to select tokens within a json string and permits the use of wildcards. 此功能允许您在json字符串中选择令牌,并允许使用通配符。
Here's some code that will deserialize your sample by selecting past the 1,2,3...N in the json: 这是一些通过选择json中的1,2,3 ... N反序列化您的样本的代码:
public static IEnumerable<DriverMaster> Deserialize(string json)
{
return JToken.Parse(json).SelectTokens("*")
.Select(jToken => jToken.ToObject<DriverMaster>());
}
The *
basically says to select all tokens after the root, so it's selecting the values associated with 1, 2, 3.. etc... Here's another SO answer that shows a more complicated usage of the SelectTokens
method. *
基本上是说要选择根之后的所有令牌,因此它正在选择与1、2、3等相关的值。这是另一个 SO答案,它显示SelectTokens
方法的更复杂用法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.