[英]C# Deserialize list in JSON
Suppose I have JSON like this, how can I model my class for deserialization? 假设我有这样的JSON,如何为反序列化为类建模?
I have no problems to model class for standard attribute like "dummy" or normal arrays, but in this case, my "links" array is a list of items with different name ("addons", "conditions", "conversion", etc.). 对于标准属性(例如“虚拟”或普通数组)的类建模,我没有问题,但是在这种情况下,我的“链接”数组是具有不同名称(“附加项”,“条件”,“转换”等的项的列表) )。
"dummy": "1",
"links": {
"addons": {
"href": "/16071d9f-efec-4282-a42e-a495eea76ae0/offers/031C9E47-4802-4248-838E-778FB1D2CC05/addons",
"method": "GET"
},
"conditions": {
"href": "/16071d9f-efec-4282-a42e-a495eea76ae0/offers/031C9E47-4802-4248-838E-778FB1D2CC05/conditions",
"method": "GET"
},
"conversions": {
"href": "/16071d9f-efec-4282-a42e-a495eea76ae0/offers/031C9E47-4802-4248-838E-778FB1D2CC05/conversions",
"method": "GET"
},
"list_prices": {
"href": "/16071d9f-efec-4282-a42e-a495eea76ae0/offers/031C9E47-4802-4248-838E-778FB1D2CC05/list-prices",
"method": "GET"
},
"mutual_exclusion": {
"href": "/16071d9f-efec-4282-a42e-a495eea76ae0/offers/031C9E47-4802-4248-838E-778FB1D2CC05/mutually-exclusive-offers",
"method": "GET"
},
"prerequisites": {
"href": "/16071d9f-efec-4282-a42e-a495eea76ae0/offers/031C9E47-4802-4248-838E-778FB1D2CC05/prerequisites",
"method": "GET"
},
"product": {
"href": "/16071d9f-efec-4282-a42e-a495eea76ae0/products/f245ecc8-75af-4f8e-b61f-27d8114de5f3",
"method": "GET"
}
},
Assuming you are specifically looking for the set of LinkTypes
if you will, in your JSON, could you use something like the following, and execute the Deserialize
on the RootObject
? 假设您要查找的是
LinkTypes
集合,是否可以在JSON中使用类似以下内容的代码,并在RootObject
上执行Deserialize
?
Check out the output on the Console pane on the fiddle page. 在小提琴页面的“控制台”窗格上签出输出。
public class Link
{
public string Href { get; set; }
public string Method { get; set; }
}
public class Links
{
[JsonProperty("addons")]
public Link Addons { get; set; }
[JsonProperty("conditions")]
public Link Conditions { get; set; }
[JsonProperty("conversions")]
public Link Conversions { get; set; }
[JsonProperty("list_prices")]
public Link ListPrices { get; set; }
[JsonProperty("mutual_exclusion")]
public Link MutualExclusion { get; set; }
[JsonProperty("prerequisites")]
public Link Prerequisites { get; set; }
[JsonProperty("product")]
public Link Product { get; set; }
}
public class RootObject
{
public string dummy { get; set; }
public Links links { get; set; }
}
and then execute the Deserializer
like so. 然后像这样执行
Deserializer
。
var myDummyLinksList = JsonConvert.DeserializeObject<RootObject>(jsonText);
where jsonText
contains the json string you have listed in your example: 其中
jsonText
包含您在示例中列出的json字符串:
However, if you List
of links
objects is dynamic and the number of objects inside varies and you need to capture all of them, then you might have to write a custom Converter that inherits from the JsonConverter
object.但是,如果
links
List
的对象是动态的,并且内部对象的数量各不相同,并且您需要捕获所有这些对象, 那么您可能必须编写一个继承自 then use the answer that @mfarouk has posted. JsonConverter
对象的自定义Converter 。然后使用@mfarouk发布的答案 。
I forked my dotNet Fiddle and implemented his solution and it works like a boss for the dynamic case! 我分叉了dotNet Fiddle并实施了他的解决方案,它像动态案例的老板一样工作!
Hope this helps! 希望这可以帮助!
the links
attribute could be parsed as key, value dictionary , the class can be like links
属性可以解析为键,值字典,类可以像
public class JSONClass
{
public string dummy { get; set; }
public Dictionary<string, Link> links;
public class Link
{
public string Href { get; set; }
public string Method { get; set; }
}
}
then de-serialized as 然后反序列化为
var obj = Newtonsoft.Json.JsonConvert.DeserializeObject<JSONClass>(JSON);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.