Reputation: 89
ScreenShot Of Error i have a json file which contains some data and i want to parse it but every time when i want to Deserialize Object it give me this error "Unable to find a constructor. A Class should either have a default constructor, one constructor with arguments or a constructor marked with json constructor i dont know what to do because i did not work with json before it is my first time working with json. here is my 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"
]
}
}
here is my model class
public class Addition
{
public List<string> Easy { get; set; }
public List<string> Medium { get; set; }
public List<string> Difficult { get; set; }
public Addition() { }
}
here is my function in which i am Deserialize object
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);
}
}
Upvotes: 0
Views: 5997
Reputation: 376
The exception occurs because you try to convert your JSON content into the wrong object. As your JSON contains an attribute "Addition" in top, it is considered as a property of some parent class. So it expects some class like this:
namespace JsonStuff
{
public class SomeParentClass
{
public Addition AdditionObject { get; set; }
public SomeParentClass()
{
}
}
}
If you try it the other way serializing an object from type "Addition" and have a look on the outcoming json string, you will see the difference:
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);
The result will be:
{
"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"
]
}
and there you see the difference: There is no "Addition" property on top.
Upvotes: 0
Reputation: 38757
You need a root object:
public class RootObject
{
public Addition Addition { get; set; }
}
var result = JsonConvert.DeserializeObject<RootObject>(json);
Console.WriteLine(result.Addition.Easy.First());
And it even works with your old code with the [JsonConstructor]
attribute.
Simply put: your C# classes must match the JSON you hope to deserialize.
Imagine you have the following JSON:
{
"person": {
"name": "John"
}
}
You cannot simply deserialize it into the following class:
public class Person
{
public string Name { get; set; }
}
Why? Because the "person" object is not your root object. The above class serialized as JSON simply looks like this:
{
"name": "John"
}
So to deserialize the first sample above, we need a root object:
public class RootObject
{
public Person Person { get; set; }
}
public class Person
{
public string Name { get; set; }
}
And now we can deserialize that JSON (the one that contains the "person" key) to RootObject
JSON is really simple - things just have to match.
Upvotes: 0
Reputation: 39277
Paste your Json as C# using Edit - Paste Special - Paste JSON as C# or use one of the online JSON to C# converters and you will see the issue:
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; }
}
Upvotes: 2