簡體   English   中英

C#制作一個JSON字符串,其中包含null對象的屬性

[英]C# making a JSON string that includes properties of objects that are null

我必須完全提交此JSON或它不起作用。 雖然我可以在另一個成員的幫助下創建大部分內容,但我找不到有關處理null的信息。 提交null值時,它將不會通過api運行。

這是我必須創建的JSON

    {
            "resourceType": "Bundle",
            "type": "message",
            "entry": [
            {
                            "resource": {
                                            "resourceType": "MessageHeader",
                                            "timestamp": "2016-12-29T08:00:00-07:00",
                                            "id": "Test1",
                                            "event":
                                                            { "code": "diagnosticreport-provide" },
                                            "source":
                                                            { "endpoint": "http://yourdomain.com/api" },
                                            "destination": [
                                                            { "endpoint": "https://api.com/api/$process-message" }
                                            ]
                            }
            },
            {
                            "resource": {
                                            "resourceType" : "DiagnosticReport",
                                            "extension" : [{
                                                            "url" : "DiagnosticReportDefinition",
                                                            "extension" : [
                                                                            { "url" : "useNewMedications", "valueBoolean": "false" },
                                                                            { "url" : "providePDFReport", "valueBoolean": "false" },
                                                                            { "url" : "returnDetectedIssues", "valueBoolean": "true" },
                                                                            { "url" : "returnObservations", "valueBoolean": "true" },
                                                                            { "url" : "returnMedications", "valueBoolean": "true" },
                                                                            { "url" : "returnDosingGuidance", "valueBoolean": "true" },
                                                                            { "url" : "includePIMTable", "valueBoolean": "true" },
                                                                            { "url" : "includeDDIData", "valueBoolean": "false" },
                                                                            { "url" : "reportId", "valueString": "" }
                                                            ]
                                            }]
                            }              
            },
            {
                            "resource":{ 
                                            "resourceType": "ProcedureRequest", 
                                            "id": "17" 
                            }
            }]

}

這是我嘗試執行並接近的代碼,但是在資源之間存在空值和一個逗號的問題

        using System;
using Newtonsoft.Json;

public class Program
{
    public static void Main()
    {
        var obj = new RootReq()
        {resourceType = "Bundle", type = "message", entry = new Entry[]{new Entry()
        {resource = new Resource()
        {resourceType = "MessageHeader", timestamp = DateTime.Now, id = "Test1", _event = new Event()
        {code = "diagnosticreport-provide"}, source = new Source()
        {endpoint = "http://yourdomain.com/api"}, destination = new Destination[]{new Destination()
        {endpoint = "https://api.pgxportal.com/api/$process-message"}}}}, new Entry()
        {resource = new Resource()
        {resourceType = "DiagnosticReport", extension = new Extension[]{new Extension()
        {url = "DiagnosticReportDefinition", extension = new Extension1[]{new Extension1()
        {url = "useNewMedications", valueBoolean = "false"}, new Extension1()
        {url = "providePDFReport", valueBoolean = "false"}, new Extension1()
        {url = "returnDetectedIssues", valueBoolean = "true"}, new Extension1()
        {url = "returnObservations", valueBoolean = "true"}, new Extension1()
        {url = "returnMedications", valueBoolean = "true"}, new Extension1()
        {url = "returnDosingGuidance", valueBoolean = "true"}, new Extension1()
        {url = "includePIMTable", valueBoolean = "true"}, new Extension1()
        {url = "includeDDIData", valueBoolean = "false"}, new Extension1()
        {url = "reportId", valueString = ""}, }}}}}, new Entry()
        {resource = new Resource()
        {resourceType = "ProcedureRequest", id = "17"}}}};
        var str = JsonConvert.SerializeObject(obj, Formatting.Indented);
        Console.WriteLine(str);
    }
}

public class RootReq
{
    public string resourceType
    {
        get;
        set;
    }

    public string type
    {
        get;
        set;
    }

    public Entry[] entry
    {
        get;
        set;
    }
}

public class Entry
{
    public Resource resource
    {
        get;
        set;
    }
}

public class Resource
{
    public string resourceType
    {
        get;
        set;
    }

    public DateTime timestamp
    {
        get;
        set;
    }

    public string id
    {
        get;
        set;
    }

    public Event _event
    {
        get;
        set;
    }

    public Source source
    {
        get;
        set;
    }

    public Destination[] destination
    {
        get;
        set;
    }

    public Extension[] extension
    {
        get;
        set;
    }
}

public class Event
{
    public string code
    {
        get;
        set;
    }
}

public class Source
{
    public string endpoint
    {
        get;
        set;
    }
}

public class Destination
{
    public string endpoint
    {
        get;
        set;
    }
}

public class Extension
{
    public string url
    {
        get;
        set;
    }

    public Extension1[] extension
    {
        get;
        set;
    }
}

public class Extension1
{
    public string url
    {
        get;
        set;
    }

    public string valueBoolean
    {
        get;
        set;
    }

    public string valueString
    {
        get;
        set;
    }
}

在這里,此代碼產生的錯誤json看起來像什么

      {
  "resourceType": "Bundle",
  "type": "message",
  "entry": [
    {
      "resource": {
        "resourceType": "MessageHeader",
        "timestamp": "2017-05-24T06:45:36.0632742+00:00",
        "id": "Test1",
        "_event": {
          "code": "diagnosticreport-provide"
        },
        "source": {
          "endpoint": "http://yourdomain.com/api"
        },
        "destination": [
          {
            "endpoint": "https://api.pgxportal.com/api/$process-message"
          }
        ],
        "extension": null
      }
    },
    {
      "resource": {
        "resourceType": "DiagnosticReport",
        "timestamp": "0001-01-01T00:00:00",
        "id": null,
        "_event": null,
        "source": null,
        "destination": null,
        "extension": [
          {
            "url": "DiagnosticReportDefinition",
            "extension": [
              {
                "url": "useNewMedications",
                "valueBoolean": "false",
                "valueString": null
              },
              {
                "url": "providePDFReport",
                "valueBoolean": "false",
                "valueString": null
              },
              {
                "url": "returnDetectedIssues",
                "valueBoolean": "true",
                "valueString": null
              },
              {
                "url": "returnObservations",
                "valueBoolean": "true",
                "valueString": null
              },
              {
                "url": "returnMedications",
                "valueBoolean": "true",
                "valueString": null
              },
              {
                "url": "returnDosingGuidance",
                "valueBoolean": "true",
                "valueString": null
              },
              {
                "url": "includePIMTable",
                "valueBoolean": "true",
                "valueString": null
              },
              {
                "url": "includeDDIData",
                "valueBoolean": "false",
                "valueString": null
              },
              {
                "url": "reportId",
                "valueBoolean": null,
                "valueString": ""
              }
            ]
          }
        ]
      }
    },
    {
      "resource": {
        "resourceType": "ProcedureRequest",
        "timestamp": "0001-01-01T00:00:00",
        "id": "17",
        "_event": null,
        "source": null,
        "destination": null,
        "extension": null
      }
    }
  ]
}

嘗試這個:

 var str= JsonConvert.SerializeObject(obj,
                            Newtonsoft.Json.Formatting.Indented,
                            new JsonSerializerSettings
                            {
                                NullValueHandling = NullValueHandling.Ignore
                            });

現在工作

        using System;
using Newtonsoft.Json;

public class Program
{
    public static void Main()
    {
        var obj = new RootReq()
        {resourceType = "Bundle", type = "message", entry = new Entry[]{new Entry()
        {resource = new Resource()
        {resourceType = "MessageHeader", 
         timestamp = Convert.ToString(DateTime.Now),
         id = "Test1", 
         _event = new Event()
        {code = "diagnosticreport-provide"}, source = new Source()
        {endpoint = "http://yourdomain.com/api"}, destination = new Destination[]{new Destination()
        {endpoint = "https://api.com/api/$process-message"}}}}, new Entry()
        {resource = new Resource()
        {resourceType = "DiagnosticReport",
         timestamp = null,
         extension = new Extension[]{
            new Extension()

        {url = "DiagnosticReportDefinition", extension = new Extension1[]{new Extension1()
        {url = "useNewMedications", valueBoolean = "false"}, new Extension1()
        {url = "providePDFReport", valueBoolean = "false"}, new Extension1()
        {url = "returnDetectedIssues", valueBoolean = "true"}, new Extension1()
        {url = "returnObservations", valueBoolean = "true"}, new Extension1()
        {url = "returnMedications", valueBoolean = "true"}, new Extension1()
        {url = "returnDosingGuidance", valueBoolean = "true"}, new Extension1()
        {url = "includePIMTable", valueBoolean = "true"}, new Extension1()
        {url = "includeDDIData", valueBoolean = "false"}, new Extension1()
        {url = "reportId", valueString = ""}, }}}}}, new Entry()
        {resource = new Resource()
        {resourceType = "ProcedureRequest", id = "17"}}}};
        var str = JsonConvert.SerializeObject(obj, Formatting.Indented, new JsonSerializerSettings { 
                                NullValueHandling = NullValueHandling.Ignore
                            });
        Console.WriteLine(str);
    }
}

public class RootReq
{
    public string resourceType
    {
        get;
        set;
    }

    public string type
    {
        get;
        set;
    }

    public Entry[] entry
    {
        get;
        set;
    }
}

public class Entry
{
    public Resource resource
    {
        get;
        set;
    }
}

public class Resource
{
    public string resourceType
    {
        get;
        set;
    }

    public String timestamp
    {
        get;
        set;
    }

    public string id
    {
        get;
        set;
    }

    public Event _event
    {
        get;
        set;
    }

    public Source source
    {
        get;
        set;
    }

    public Destination[] destination
    {
        get;
        set;
    }

    public Extension[] extension
    {
        get;
        set;
    }
}

public class Event
{
    public string code
    {
        get;
        set;
    }
}

public class Source
{
    public string endpoint
    {
        get;
        set;
    }
}

public class Destination
{
    public string endpoint
    {
        get;
        set;
    }
}

public class Extension
{
    public string url
    {
        get;
        set;
    }

    public Extension1[] extension
    {
        get;
        set;
    }
}

public class Extension1
{
    public string url
    {
        get;
        set;
    }

    public string valueBoolean
    {
        get;
        set;
    }

    public string valueString
    {
        get;
        set;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM