Deserialization of JSON.Net returns 'null'

泄露秘密 提交于 2019-12-08 10:13:55

问题


I am using JSON.Net to Deserialize a JSON string. The JSON string is

string testJson = @"{
                    ""Fruits"": {
                        ""Apple"": {
                            ""color"": ""red"",
                            ""size"": ""round""                               
                        },
                        ""Orange"": {
                            ""Properties"": {
                                ""color"": ""red"",
                                ""size"": ""round"" 
                            }
                        }
                    }
                }";

and my code is

public class Fruits
{
    public Apple apple { get; set; }
    public Orange orange { get; set; }
}

public class Apple
{
    public string color { get; set; }
    public string size { get; set; }            
}

public class Orange
{
    public Properties properties { get; set; }
}

public class Properties
{
    public string color { get; set; }
    public string size { get; set; }   
}   

I am trying to Deserialize this with the following code

Fruits result = JsonConvert.DeserializeObject<Fruits>(testJson);

I have a problem in my result that Fruits with Apple and Orange has null instead of their Properties , color , size.


回答1:


Assuming you can't change the json, you need to create a new FruitsWrapper class that has a Fruits property

public class FruitsWrapper
{
    public Fruits Fruits { get; set; }
}

and deserialize the json into an instance of FruitsWrapper instead of Fruits

FruitsWrapper result = JsonConvert.DeserializeObject<FruitsWrapper>(testJson);

Working demo: https://dotnetfiddle.net/nQitSD




回答2:


The problem is that the outermost bracers in your Json corresponds to the type you're trying to deserialize.

So this:

string testJson = @"{
                      ""Fruits"": { ... }
                    }";

Corresponds to Fruits since that's what you're trying to deserialize.

string testJson = @"{
                      ""Fruits"": { ... }
                        ^--+-^
                           |
                           +-- this is assumed to be a property of this --+
                                                                          |
                                                 +------------------------+
                                                 |
                                              v--+-v
Fruits result = JsonConvert.DeserializeObject<Fruits>(testJson);

However, the Fruits type does not have a property named Fruits and thus nothing is deserialized.

If the Json cannot be changed, you need to create a container type that you deserialize instead, like this:

public class Container
{
    public Fruits Fruits { get; set; }
}

Then you can deserialize that:

Container result = JsonConvert.DeserializeObject<Container>(testJson);



回答3:


the JSON string should be:

string testJson = @"{ 
                        ""Apple"": {
                            ""color"": ""red"",
                            ""size"": ""round""},
                        ""Orange"": { 
                            ""Properties"": {
                                ""color"": ""red"",
                                ""size"": ""round"" }
                                }
                    }";

to be deserialized with your class




回答4:


You JSON string isn't correct (as others have pointed out) but I'm adding this to assist you with finding the correct format. Given you have a fruit object like this:

var fruit = new Fruits
{
    apple = new Apple { color = "red", size = "massive" },
    orange = new Orange { properties = new Properties { color = "orange", size = "mediumish" } }
};

You can then serialise this to JSON:

var testJson = JsonConvert.SerializeObject(fruit);

And now you can see the serialised output will look like this: (slightly formatted)

{"apple":
    {"color":"red","size":"massive"},
 "orange":
    {"properties":
        {"color":"orange","size":"mediumish"}}}

And this will easily deserialise back into your object:

fruit = JsonConvert.DeserializeObject<Fruits>(testJson);


来源:https://stackoverflow.com/questions/27128538/deserialization-of-json-net-returns-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!