C# Json反序列化对象
嗨,我想反序列化这个Json:C# Json反序列化对象,c#,json,C#,Json,嗨,我想反序列化这个Json: { "engine_name": "Hermle", "criterion": { "squareness": { "name": "perpendicularité", "theoretical": "5", "expected": "5" }, "backlash": { "name": "jeu à l'inversion", "theoretical": "5",
{
"engine_name": "Hermle",
"criterion": {
"squareness": {
"name": "perpendicularité",
"theoretical": "5",
"expected": "5"
},
"backlash": {
"name": "jeu à l'inversion",
"theoretical": "5",
"expected": "5"
},
"straightness": {
"name": "rectitude",
"theoretical": "1",
"expected": "1"
},
"spindle-runout": {
"name": "faux rond broche",
"theoretical": "5",
"expected": "5"
},
"circularity": {
"name": "circularité",
"theoretical": "5",
"expected": "5"
}
}
}
我有我的类对象:
public class SmartDevice
{
public string Engine_name { get; set; }
public Object criterion { get; set; }
}
这很好,但是我不能为Criteria对象设置Count属性,所以我想要一个Criteria列表,但我不知道如何做到这一点。在定义问题时,不可能从Criteria获取Count,因为它是一个对象,而不是列表或数组。 要获得标准列表,您需要以下内容:
{
"engine_name": "Hermle",
"criterion": [
{
"type": "squareness",
"name": "perpendicularité",
"theoretical": "5",
"expected": "5"
},
{
"type": "circularity",
"name": "circularité",
"theoretical": "5",
"expected": "5"
}
]
}
public class SmartDevice
{
public string Engine_name { get; set; }
public List<Object> criterion { get; set; }
}
课程应该是这样的:
{
"engine_name": "Hermle",
"criterion": [
{
"type": "squareness",
"name": "perpendicularité",
"theoretical": "5",
"expected": "5"
},
{
"type": "circularity",
"name": "circularité",
"theoretical": "5",
"expected": "5"
}
]
}
public class SmartDevice
{
public string Engine_name { get; set; }
public List<Object> criterion { get; set; }
}
公共级智能设备
{
公共字符串引擎_name{get;set;}
公共列表条件{get;set;}
}
您可以使用字典
或代替对象
使用自定义类型,如CriteriaObject
可以声明为列表标准,而不是???@apomene,但它不是列表。使用int理论上的、预期的呢?JSON中只有两个相同的准则当我将准则声明为一个列表时,它会说:无法将当前JSON对象(例如{“name\”:“value\”})反序列化为类型“System.Collections.Generic.List`1[System.object]”,因为该类型需要JSON数组(例如[1,2,3])要正确反序列化。\r\n若要修复此错误,请将JSON更改为JSON数组(例如[1,2,3]),或更改反序列化类型,使其成为可以从JSON对象反序列化的正常.NET类型。还可以将JsonObjectAttribute添加到类型中,以强制它从JSON对象反序列化。这是因为Criteria是一个对象而不是列表。仅供参考:JSON中的任何对象都可以通过字典
表示。如果我想自定义类型,我该如何做?我尝试了以下方法:公共部分类标准{public string name{get;set;}public Crit{get;set;}}}公共部分类标准{public string name{get;set;}公共字符串理论{get;set;}应为公共字符串{get;set;}}为什么是部分的?顺便说一句:如果你使用Newtonsoft.Json 10+(?),你不需要写小写:expected
(Json)=>expected
(c#)不需要部分,我不知道小写,谢谢