我必须完全提交这个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"
}
}]
}
这是我尝试这样做并接近的代码,但资源之间的空值和一个逗号存在问题
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;
}
}