Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#生成包含空对象属性的JSON字符串_C#_Json_Object_Null - Fatal编程技术网

C#生成包含空对象属性的JSON字符串

C#生成包含空对象属性的JSON字符串,c#,json,object,null,C#,Json,Object,Null,我必须完全提交这个JSON,否则它将无法工作。虽然我能够在另一个成员的帮助下创建它的大部分,但我找不到有关处理空值的信息。当它提交空值时,它将不会通过api运行 以下是我必须创建的JSON { "resourceType": "Bundle", "type": "message", "entry": [ { "resource": {

我必须完全提交这个JSON,否则它将无法工作。虽然我能够在另一个成员的帮助下创建它的大部分,但我找不到有关处理空值的信息。当它提交空值时,它将不会通过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" 
                            }
            }]
}

下面是我尝试执行此操作的代码,结果很接近,但在资源之间使用null和一个逗号时出现问题

        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;
    }
}

我还遇到了c#不允许将事件对象的实例用作命名事件not#U Event的问题如果您想忽略空值,那么这可能会有所帮助:谢谢jason.kaisersmith修复了这方面的大部分问题。直到错误的括号结束和一个错误的名称\u事件您可以使用属性更改属性/字段序列化或反序列化的名称:
DateTime
从不
null
。如果希望它是
null
,则必须使用可为null的类型
null
DateTime?