找不到构造函数

时间:2019-04-12 06:52:22

标签: c# json xamarin.forms json.net

ScreenShot Of Error我有一个包含一些数据的json文件,我想解析它,但是每次当我想反序列化对象时,它都会给我这个错误“无法找到构造函数。一个类应该具有一个默认构造函数,带参数的构造函数或标有json构造函数的构造函数,我不知道该怎么做,因为在我第一次使用json之前,我没有使用json。 这是我的json:

{
   "Addition":
   {
       "Easy": [

                "New York Bulls",
                "Los Angeles Kings",
                "Golden State Warriros",
                "Huston Rocket"
            ],
      "Medium":[
                "New York Bulls",
                "Los Angeles Kings",
                "Golden State Warriros",
                "Huston Rocket"
      ],
       "Difficult": [
               "New York Bulls",
               "Los Angeles Kings",
               "Golden State Warriros",
               "Huston Rocket"
      ] 
   }
}

这是我的模型课

public class Addition
{

     public List<string> Easy { get; set; }
        public List<string> Medium { get; set; }
        public List<string> Difficult { get; set; }

    public Addition() { }

}

这是我反序列化对象的功能

private void ReadJson()
    {
        var assembly = typeof(WordProblemsScreen).GetTypeInfo().Assembly;
        Stream stream = 
assembly.GetManifestResourceStream("MathRandomizer.demo.json");

        using (var reader = new System.IO.StreamReader(stream))
        {

            string json = reader.ReadToEnd();

            JObject jObject = JObject.Parse(json);
            JToken jUser = jObject["Addition"];


            var addition = JsonConvert.DeserializeObject<Addition>(json);




        }
    }

3 个答案:

答案 0 :(得分:2)

使用编辑-选择性粘贴-将JSON粘贴为C#粘贴Json作为C#,或使用在线JSON到C#转换器之一,您将看到以下问题:

public class Addition
{
    public List<string> Easy { get; set; }
    public List<string> Medium { get; set; }
    public List<string> Difficult { get; set; }
}

public class RootObject
{ 
   public Addition Addition { get; set; }
}

答案 1 :(得分:0)

您需要一个根对象:

public class RootObject
{
    public Addition Addition { get; set; }
}

var result = JsonConvert.DeserializeObject<RootObject>(json);
Console.WriteLine(result.Addition.Easy.First());

Try it online

甚至还有works和带有[JsonConstructor]属性的旧代码。

简单地说:您的C#类必须与您希望反序列化的JSON匹配。

假设您有以下JSON:

{
    "person": {
        "name": "John"
    }
}

不能将其反序列化为以下类:

public class Person
{
    public string Name { get; set; }
}

为什么?因为“人”对象不是您的根对象。上面序列化为JSON的类看起来像这样:

{
    "name": "John"
}

因此要反序列化上面的第一个示例,我们需要一个根对象:

public class RootObject
{
    public Person Person { get; set; }
}

public class Person
{
    public string Name { get; set; }
}

现在我们可以将该JSON(包含“ person”密钥的JSON)反序列化为RootObject

JSON非常简单-事情必须匹配。

答案 2 :(得分:0)

发生异常是因为您尝试将JSON内容转换为错误的对象。由于您的JSON顶部包含一个属性“ Addition”,因此它被视为某些父类的属性。因此,它需要这样的类:

namespace JsonStuff
{
    public class SomeParentClass
    {
        public Addition AdditionObject { get; set; }

        public SomeParentClass()
        {

        }
    }
}

如果您尝试通过另一种方式从“ Addition”类型序列化对象并查看即将到来的json字符串,则会看到不同之处:

var filledObject = new Addition()
{
    Easy = new List<string>()
    {
        "New York Bulls",
        "Los Angeles Kings",
        "Golden State Warriros",
        "Huston Rocket"
    },
    Medium = new List<string>()
    {
        "New York Bulls",
        "Los Angeles Kings",
        "Golden State Warriros",
        "Huston Rocket"
    },
    Difficult = new List<string>()
    {
        "New York Bulls",
        "Los Angeles Kings",
        "Golden State Warriros",
        "Huston Rocket"
    }
};

var serializedObject = JsonConvert.SerializeObject(filledObject);

结果将是:

{
    "Easy":
    [
        "New York Bulls",
        "Los Angeles Kings",
        "Golden State Warriros",
        "Huston Rocket"
    ],
    "Medium":
    [
        "New York Bulls",
        "Los Angeles Kings",
        "Golden State Warriros",
        "Huston Rocket"
    ],
    "Difficult":
    [
        "New York Bulls",
        "Los Angeles Kings",
        "Golden State Warriros",
        "Huston Rocket"
    ]
}

您会发现区别:顶部没有“ Addition”属性。