杰森反序列化对象

时间:2018-08-07 12:45:10

标签: c# json

嗨,我想反序列化这个Json:

{
  "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; }
}

这很好用,但是我无法为条件对象提供计数属性,因此我想列出条件列表,但我不知道该怎么做。

2 个答案:

答案 0 :(得分:2)

您可以使用Dictionary<string, object>,也可以使用object之类的自定义类型来代替CriteriaObject

答案 1 :(得分:0)

在定义问题时,不可能从条件中获取计数,因为它是对象而不是列表或数组。 为了拥有条件列表,您需要这样的东西:

{
  "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; }
}