简体   繁体   English

如何使用Newtonsoft.Json反序列化null JSON对象?

[英]How to deserialize null JSON object using Newtonsoft.Json?

So I have a JSON file that has an object nested within it. 所以我有一个JSON文件,其中嵌套了一个对象。 It looks like this: 它看起来像这样:

{ "MyObject" : { "prop1": "Value1", "prop2" : "Value2"}
  "Message" : "A message"}

Sometimes, the object nested inside the JSON may be null. 有时,嵌套在JSON中的对象可能为null。 So for example, 所以,例如,

{ "MyObject" : null
  "Message" : "A message"}

I'm trying to deserialize this object using Newtonsoft.Json, into an object (let's call it JsonObject) which has two properties: "MyObject" and "Message". 我正在尝试使用Newtonsoft.Json将此对象反序列化为一个对象(让我们称之为JsonObject),该对象具有两个属性:“MyObject”和“Message”。 Message is just a string, but MyObject is an object which has a few other (private) properties as well as a constructor: Message只是一个字符串,但MyObject是一个具有一些其他(私有)属性以及构造函数的对象:

public class MyObject
{
    public MyObject(string prop1, string prop2)
    {
        this.prop1= prop1;
        this.prop2= prop2;
    }

    public string prop1 { get; private set; }

    public string prop2 { get; private set; }

JsonObject looks like this: JsonObject看起来像这样:

public class JsonObject
{
    public MyObject MyObject { get; set; }
    public string Message { get; set; }
}

And my code to deserialize looks like this: 我的反序列化代码如下所示:

using (StreamReader reader = new StreamReader(filename))
                    {
                        string jsonFile = reader.ReadToEnd();
                        return JsonConvert.DeserializeObject<JsonObject>(jsonFile);
                    }

But it is throwing an error: 但这是一个错误:

Exception: Object reference not set to an instance of an object

This happens when the MyObject object is null. 当MyObject对象为null时会发生这种情况。 I printed jsonFile, and this is the output: 我打印了jsonFile,这是输出:

{"MyObject": null, "Message": "sample message"}

What I want is for MyObject to deserialize to null. 我想要的是MyObject反序列化为null。 So when the code runs, the end result is a JsonObject with MyObject = null and Message = "sample message". 因此,当代码运行时,最终结果是一个JsonObject,MyObject = null,Message =“sample message”。 How do I do this using Newtonsoft.Json? 我如何使用Newtonsoft.Json执行此操作?

I created a small test with what you've provided above and found that MyObject and Message were missing a comma between them. 我用上面提供的内容创建了一个小测试,发现MyObject和Message在它们之间缺少逗号。 Otherwise everything worked fine. 否则一切都很好。 Checking the objects via debug shows everything parses as it should. 通过debug检查对象显示所有内容都应该解析。

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;

namespace UnitTestProject1
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            const string json1 = "{ \"MyObject\" : { \"prop1\": \"Value1\", \"prop2\" : \"Value2\"}, \"Message\" : \"A message\"}";
            const string json2 = "{ \"MyObject\" : null, \"Message\" : \"A message\"}";

            var obj1 = JsonConvert.DeserializeObject<JsonObject>(json1);
            var obj2 = JsonConvert.DeserializeObject<JsonObject>(json2);

            Assert.IsInstanceOfType(obj1, typeof(JsonObject));
            Assert.IsInstanceOfType(obj2, typeof(JsonObject));
        }
    }


    public class MyObject
    {
        public MyObject(string prop1, string prop2)
        {
            this.prop1 = prop1;
            this.prop2 = prop2;
        }

        public string prop1 { get; private set; }

        public string prop2 { get; private set; }
    }

    public class JsonObject
    {
        public MyObject MyObject { get; set; }
        public string Message { get; set; }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM