格式化以下 JSON 的正确方法是什么?
What is correct way formatting the below JSON?
{
"id": "bf96aa5d-daf7-4de6-8ab0-c5ceb57b43af",
"created": "1582519813359",
"result": {
"status": "Succeed"
},
"amount": 6789,
"provider_data": {
"provider_name": "Magellan",
"response_code": "00",
"description": "Successful",
"raw_response": "{\"raw_response\":\"
<soap:Envelope xmlns:soap=\\"http://schemas.xmlsoap.org/soap/envelope/\\">
<soap:Body><ns2:doTransactionResponse xmlns:ns2=\\"http://soap.api.controller.web.payjar.com/\\"><return>
<merchantReference>e5638752-be88-423b-8db8-e181db4825651582519812683</merchantReference>
<MerReference>14976972006762</MerReference>
<resultCode>00</resultCode>
<resultMessage>Successful</resultMessage><successful>true</successful></return>
</ns2:doTransactionResponse></soap:Body></soap:Envelope>\"}",
"transaction_id": "14976972006762"
},
以上是我收到的 JSON 回复。直到 Raw_respose 为 JSON,我可以将其读作 JSON。在那之后,内部 raw_Reponse 我既不能读作 JSON 也不能读作 XML 也不能读作 SOAP。格式到底是什么以及我如何在 C#
中读取它
我需要提取但无法提取任何格式的内部 raw_response
您可以通过执行几个反序列化调用来进行 SOAP 调用。
- 反序列化 json, main Json
- 反序列化 json、raw_response
- 反序列化 xml、raw_response
- 访问您需要的元素。
类 您需要 SOAP XML
[Serializable()]
[DesignerCategory("code")]
[XmlType(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
[XmlRoot(Namespace = "http://schemas.xmlsoap.org/soap/envelope/", IsNullable = false)]
public partial class Envelope
{
public EnvelopeBody Body { get; set; }
}
[Serializable()]
[DesignerCategory("code")]
[XmlType(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public partial class EnvelopeBody
{
[XmlElement(Namespace = "http://soap.api.controller.web.payjar.com/")]
public doTransactionResponse doTransactionResponse { get; set; }
}
[Serializable()]
[DesignerCategory("code")]
[XmlType(AnonymousType = true, Namespace = "http://soap.api.controller.web.payjar.com/")]
[XmlRoot(Namespace = "http://soap.api.controller.web.payjar.com/", IsNullable = false)]
public partial class doTransactionResponse
{
[XmlElement(Namespace = "")]
public @return @return { get; set; }
}
[Serializable()]
[DesignerCategory("code")]
[XmlType(AnonymousType = true)]
[XmlRoot(Namespace = "", IsNullable = false)]
public partial class @return
{
public string merchantReference { get; set; }
public ulong MerReference { get; set; }
public byte resultCode { get; set; }
public string resultMessage { get; set; }
public bool successful { get; set; }
}
要在 Main 中使用的代码
public static void Main(string[] args)
{
string json = @"{
""id"": ""bf96aa5d-daf7-4de6-8ab0-c5ceb57b43af"",
""created"": ""1582519813359"",
""result"": {
""status"": ""Succeed""
},
""amount"": 6789,
""provider_data"": {
""provider_name"": ""Magellan"",
""response_code"": ""00"",
""description"": ""Successful"",
""raw_response"": ""{\""raw_response\"":\""
<soap:Envelope xmlns:soap=\\""http://schemas.xmlsoap.org/soap/envelope/\\"">
<soap:Body><ns2:doTransactionResponse xmlns:ns2=\\""http://soap.api.controller.web.payjar.com/\\""><return>
<merchantReference>e5638752-be88-423b-8db8-e181db4825651582519812683</merchantReference>
<MerReference>14976972006762</MerReference>
<resultCode>00</resultCode>
<resultMessage>Successful</resultMessage><successful>true</successful></return>
</ns2:doTransactionResponse></soap:Body></soap:Envelope>\""}"",
""transaction_id"": ""14976972006762""
}
}";
// Convert the complete json first.
var jobj = JObject.Parse(json);
var mainRawResponse = jobj["provider_data"]["raw_response"].ToString();
// Convert the string version of the object
var soapResponse= JObject.Parse(mainRawResponse)["raw_response"].ToString();
Envelope data = null;
// Conver the SOAP response.
using (var reader = new StringReader(soapResponse))
{
XmlSerializer serializer = new XmlSerializer(typeof(Envelope));
data = (Envelope)serializer.Deserialize(reader);
}
// data variable holds the data you need from SOAP xml.
Console.WriteLine(data.Body.doTransactionResponse);
Console.WriteLine(((@return)data.Body.doTransactionResponse.@return).merchantReference);
}
{
"id": "bf96aa5d-daf7-4de6-8ab0-c5ceb57b43af",
"created": "1582519813359",
"result": {
"status": "Succeed"
},
"amount": 6789,
"provider_data": {
"provider_name": "Magellan",
"response_code": "00",
"description": "Successful",
"raw_response": "{\"raw_response\":\"
<soap:Envelope xmlns:soap=\\"http://schemas.xmlsoap.org/soap/envelope/\\">
<soap:Body><ns2:doTransactionResponse xmlns:ns2=\\"http://soap.api.controller.web.payjar.com/\\"><return>
<merchantReference>e5638752-be88-423b-8db8-e181db4825651582519812683</merchantReference>
<MerReference>14976972006762</MerReference>
<resultCode>00</resultCode>
<resultMessage>Successful</resultMessage><successful>true</successful></return>
</ns2:doTransactionResponse></soap:Body></soap:Envelope>\"}",
"transaction_id": "14976972006762"
},
以上是我收到的 JSON 回复。直到 Raw_respose 为 JSON,我可以将其读作 JSON。在那之后,内部 raw_Reponse 我既不能读作 JSON 也不能读作 XML 也不能读作 SOAP。格式到底是什么以及我如何在 C#
中读取它我需要提取但无法提取任何格式的内部 raw_response
您可以通过执行几个反序列化调用来进行 SOAP 调用。
- 反序列化 json, main Json
- 反序列化 json、raw_response
- 反序列化 xml、raw_response
- 访问您需要的元素。
类 您需要 SOAP XML
[Serializable()]
[DesignerCategory("code")]
[XmlType(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
[XmlRoot(Namespace = "http://schemas.xmlsoap.org/soap/envelope/", IsNullable = false)]
public partial class Envelope
{
public EnvelopeBody Body { get; set; }
}
[Serializable()]
[DesignerCategory("code")]
[XmlType(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public partial class EnvelopeBody
{
[XmlElement(Namespace = "http://soap.api.controller.web.payjar.com/")]
public doTransactionResponse doTransactionResponse { get; set; }
}
[Serializable()]
[DesignerCategory("code")]
[XmlType(AnonymousType = true, Namespace = "http://soap.api.controller.web.payjar.com/")]
[XmlRoot(Namespace = "http://soap.api.controller.web.payjar.com/", IsNullable = false)]
public partial class doTransactionResponse
{
[XmlElement(Namespace = "")]
public @return @return { get; set; }
}
[Serializable()]
[DesignerCategory("code")]
[XmlType(AnonymousType = true)]
[XmlRoot(Namespace = "", IsNullable = false)]
public partial class @return
{
public string merchantReference { get; set; }
public ulong MerReference { get; set; }
public byte resultCode { get; set; }
public string resultMessage { get; set; }
public bool successful { get; set; }
}
要在 Main 中使用的代码
public static void Main(string[] args)
{
string json = @"{
""id"": ""bf96aa5d-daf7-4de6-8ab0-c5ceb57b43af"",
""created"": ""1582519813359"",
""result"": {
""status"": ""Succeed""
},
""amount"": 6789,
""provider_data"": {
""provider_name"": ""Magellan"",
""response_code"": ""00"",
""description"": ""Successful"",
""raw_response"": ""{\""raw_response\"":\""
<soap:Envelope xmlns:soap=\\""http://schemas.xmlsoap.org/soap/envelope/\\"">
<soap:Body><ns2:doTransactionResponse xmlns:ns2=\\""http://soap.api.controller.web.payjar.com/\\""><return>
<merchantReference>e5638752-be88-423b-8db8-e181db4825651582519812683</merchantReference>
<MerReference>14976972006762</MerReference>
<resultCode>00</resultCode>
<resultMessage>Successful</resultMessage><successful>true</successful></return>
</ns2:doTransactionResponse></soap:Body></soap:Envelope>\""}"",
""transaction_id"": ""14976972006762""
}
}";
// Convert the complete json first.
var jobj = JObject.Parse(json);
var mainRawResponse = jobj["provider_data"]["raw_response"].ToString();
// Convert the string version of the object
var soapResponse= JObject.Parse(mainRawResponse)["raw_response"].ToString();
Envelope data = null;
// Conver the SOAP response.
using (var reader = new StringReader(soapResponse))
{
XmlSerializer serializer = new XmlSerializer(typeof(Envelope));
data = (Envelope)serializer.Deserialize(reader);
}
// data variable holds the data you need from SOAP xml.
Console.WriteLine(data.Body.doTransactionResponse);
Console.WriteLine(((@return)data.Body.doTransactionResponse.@return).merchantReference);
}