简体   繁体   English

Newtonsoft.Json.JsonConvert(object) 返回 null object

[英]Newtonsoft.Json.JsonConvert(object) returns null object

I have a 'complex' object that I want to serialize with JSon.Convert.我有一个“复杂”的 object,我想用 JSon.Convert 对其进行序列化。 As 'complex' objects go it is rather simple: Here are the objects:作为“复杂”对象 go 它相当简单:以下是对象:

The main object:主要object:

public class CustomerContactRequest
{
    private RequestHeaderArea header;
    private RequestPayloadArea payload;

    public CustomerContactRequest(string headerMessage, string npsGroup, string npsSection)
    {
        this.header = new RequestHeaderArea(headerMessage);
        this.payload = new RequestPayloadArea(npsGroup, npsSection);
    }
}

The 'header' Object: “标题” Object:

public class RequestHeaderArea
{
    private string headerMessage;
    public string HeaderMessage { get { return headerMessage; } }
   
    public RequestHeaderArea(string headerMessage)
    {
        this.headerMessage = headerMessage;
    }
}

The Payload Area:有效载荷区域:

public class RequestPayloadArea
{
    private string npsGroup;
    private string npsSection;

    public string NPSGroup { get { return npsGroup; } }
    public string NPSSection { get { return npsSection; } }
    public RequestPayloadArea(string npsGroup, string npsSection)
    {
        this.npsGroup = npsGroup;
        this.npsSection = npsSection;
    }
}

And Finally, the main process:最后,主要流程:

static void Main(string[] args)
    {
        CustomerContactRequest ccRequest = new CustomerContactRequest(
            headerMessage: "test", 
            npsGroup: "1234567", 
            npsSection: "0000");
        retrieveContactInfo(ccRequest);
        
    }
    static void retrieveContactInfo(CustomerContactRequest ccRequest)
    {
        string jsonRequest = JsonConvert.SerializeObject(ccRequest);
        // code to call service
    }

jsonRequest returns {} even though ccRequest contains the expected values.即使 ccRequest 包含预期值,jsonRequest 也会返回 {}。 What am I missing?我错过了什么?

I am expecting something like this (sans formatting):我期待这样的事情(无格式):

{
"headerArea": {
    "messageId": "test"
},
"payloadArea": {
    "group": {
        "Number": "1234567",
        "Suffix": "0000"
        }
    }
}

Implementing Chris's answer my classes now look like below (main program did not change except that I added Formatted.Indented to the SerializeObject call to make it pretty):实现 Chris 的回答,我的类现在如下所示(主程序没有改变,只是我在 SerializeObject 调用中添加了 Formatted.Indented 以使其更漂亮):

CustomerContactRequest:客户联系请求:

public class CustomerContactRequest
{
    public  RequestHeaderArea headerArea;
    public  RequestPayloadArea payloadArea;

    public CustomerContactRequest(string headerMessage, string npsGroup, string npsSection)
    {
        this.headerArea = new RequestHeaderArea(headerMessage);
        this.payloadArea = new RequestPayloadArea(npsGroup, npsSection);
    }
   
}

RequestHeaderArea:请求标头区域:

 public class RequestHeaderArea
{
    private string messageId;
    public string MessageId { get { return messageId; } }
   
    public RequestHeaderArea(string headerMessage)
    {
        this.messageId = headerMessage;
    }
   
}

RequestPayloadArea:请求有效载荷区域:

  public class RequestPayloadArea
{
    public Group group;

   
    public RequestPayloadArea(string npsGroup, string npsSection)
    {
        this.group = new Group(npsGroup, npsSection);
    }

    
}

And a new class: Group:还有一个新的 class: 组:

public class Group
{
    public string Number;
    public string Suffix;

    public Group(string npsGroup, string npsSection)
    {
        Number = npsGroup;
        Suffix = npsSection;
    }
}

Now my Json looks exactly as expected (see green text above)现在我的 Json 看起来完全符合预期(见上面的绿色文字)

SerializeObject ignores private members by default. SerializeObject默认忽略私有成员。 You can either make them public, or by adding the SerializableAttribute to your CustomerContractRequest class.您可以将它们公开,或者将SerializableAttribute添加到您的CustomerContractRequest class。

暂无
暂无

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

相关问题 尝试扩展Newtonsoft.Json.JsonConvert的SerializeObject方法 - Trying to extend SerializeObject method of Newtonsoft.Json.JsonConvert 为什么我的反序列化给我Newtonsoft.Json.JsonConvert一个错误? - Why my deserialization is giving me an error Newtonsoft.Json.JsonConvert? 由 Newtonsoft.Json.JsonConvert 从同一个类序列化 nad 反序列化 - Serialize nad Deserialize from this same Class by Newtonsoft.Json.JsonConvert JsonConvert为List返回null <object> - JsonConvert returns null for List<object> 无法从程序集 Newtonsoft.Json 加载类型“Newtonsoft.Json.JsonConvert” - Could not load type 'Newtonsoft.Json.JsonConvert' from assembly Newtonsoft.Json &#39;Newtonsoft.Json.dll&#39;和&#39;NuGetApi2.dll&#39;中都存在&#39;Newtonsoft.Json.JsonConvert&#39;类型 - The type 'Newtonsoft.Json.JsonConvert' exists in both 'Newtonsoft.Json.dll' and 'NuGetApi2.dll' Can I covert python class to dictionary or json like Newtonsoft.Json.JsonConvert() in C# - Can I covert python class to dictionary or json like Newtonsoft.Json.JsonConvert() in C# 即使使用Newtonsoft.Json.JsonConvert进行序列化,REST服务也会收到(400)错误的特殊字符请求 - Rest service getting (400) Bad Request for special characters even after serializing with Newtonsoft.Json.JsonConvert 为什么我的项目中出现 Newtonsoft.Json.JsonConvert 的 CS0433 错误? - Why is there a CS0433 error for Newtonsoft.Json.JsonConvert in my project? NewtonSoft JsonConvert对象转义字符的SerializeObject字典 - NewtonSoft JsonConvert SerializeObject Dictionary of Object Escape Chars
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM