我正在使用Newtonsoft将XML转换为JSON,其中一个API使用JSON作为输入,但使用Newtonsoft我获得了与API不兼容的不同JSON结果。
<packages>
<Package>
<trackingNumber>23780498191986</trackingNumber>
<shipCost>14</shipCost>
<shipDate>2018-04-12T15:33:39.887</shipDate>
<items>
<Item>
<dscoItemId>sdsd</dscoItemId>
<sku>sdsd</sku>
<quantity>1</quantity>
<lineNumber>1</lineNumber>
<originalLineNumber>1</originalLineNumber>
<originalOrderQuantity>1</originalOrderQuantity>
</Item>
<Item>
<dscoItemId>CCCVSD33</dscoItemId>
<sku>TESRRST123</sku>
<quantity>2</quantity>
<lineNumber>2</lineNumber>
<originalLineNumber>2</originalLineNumber>
<originalOrderQuantity>2</originalOrderQuantity>
</Item>
</items>
<shipCarrier />
<shipMethod />
<shippingServiceLevelCode>FESDPO</shippingServiceLevelCode>
<numberOfLineItems>2</numberOfLineItems>
<shipFrom>
<attention>OPERATIONS m</attention>
<firstName>SD</firstName>
<lastName>SD</lastName>
<company></company>
<address1>ADD1</address1>
<address2 />
<city>URBANCREST</city>
<region>OH</region>
<postal>43123</postal>
<country>US</country>
<phone>123.125.1265</phone>
<email />
<locationCode />
</shipFrom>
<shipTo>
<attention>SD</attention>
<firstName>DD</firstName>
<lastName>DD</lastName>
<company>ABC CORPORATION</company>
<address1>123 FINE WAY</address1>
<address2>BUILDING F-1</address2>
<city>BALTIMORE</city>
<region>MD</region>
<postal>21234</postal>
<country>US</country>
<phone>222-222-2222</phone>
<email>XYZ@ACOMP.COM</email>
<locationCode />
</shipTo>
<shipWeight>35.00</shipWeight>
</Package>
想要在我的测试应用中使用以下格式转换其中一个API
"packages": [
{
"trackingNumber": "7804981212191986",
"shipCost": 14,
"shipDate": "2018-04-12T15:33:39.887",
"items": [
{
"dscoItemId": 10312120074055,
"sku": "ABC11223",
"quantity": 1,
"lineNumber": 1,
"originalLineNumber": 1,
"originalOrderQuantity": 1
}
],
"shipCarrier": null,
"shipMethod": null,
"shippingServiceLevelCode": "FEPO",
"numberOfLineItems": 1,
"shipFrom": {
"attention": "OPERATIONS manager",
"firstName": "sd",
"lastName": "Ysd",
"company": "sd",
"address1": "Add1",
"city": "URBANCREST",
"region": "OH",
"postal": "43123",
"country": "US",
"phone": "123.212.1235"
},
"shipTo": {
"attention": "Attention: GROUP A",
"firstName": "MO",
"lastName": "WK",
"company": "ABC CORPORATION",
"address1": "123 FINE WAY",
"address2": "BUILDING F-1",
"city": "BALTIMORE",
"region": "MD",
"postal": "21234",
"country": "US",
"phone": "222-222-2222",
"email": "JOE@ACOMP.COM"
},
"shipWeight": "35.00"
}
]
但是使用Newtonsoft我得到的结果对于我在测试应用程序中消耗的API无效。
"packages": {
"Package": {
"trackingNumber": "780498193231986",
"shipCost": "14.14",
"shipDate": "2018-04-12T15:33:39.887",
"items": {
"Item": [
{
"dscoItemId": "AAVVCAACF",
"sku": "ABC123",
"quantity": "1",
"lineNumber": "1",
"originalLineNumber": "1",
"originalOrderQuantity": "1"
},
{
"dscoItemId": "CCCAAV33",
"sku": "TEST123",
"quantity": "2",
"lineNumber": "2",
"originalLineNumber": "2",
"originalOrderQuantity": "2"
}
]
},
"shipCarrier": null,
"shipMethod": null,
"shippingServiceLevelCode": "FEPO",
"numberOfLineItems": "2",
"shipFrom": {
"attention": "OPERATIONS manager",
"firstName": "asas",
"lastName": "asas",
"company": "asas",
"address1": "Add1",
"address2": null,
"city": "URBANCREST",
"region": "OH",
"postal": "43123",
"country": "US",
"phone": "125.125.1235",
"email": null,
"locationCode": null
},
"shipTo": {
"attention": "Attention: GROUP A",
"firstName": "sa",
"lastName": "sd",
"company": "ABC CORPORATION",
"address1": "123 FINE WAY",
"address2": "BUILDING F-1",
"city": "BALTIMORE",
"region": "MD",
"postal": "21234",
"country": "US",
"phone": "222-222-2222",
"email": "JOE@ACOMP.COM",
"locationCode": null
}
以下代码用于将XML转换为JSON
private static string XmlToJSONWithJSONNet(String xmlDoc)
{
string jsonString = string.Empty;
XmlDocument xd = new XmlDocument();
xd.LoadXml(Convert.ToString(xmlDoc));
jsonString = JsonConvert.SerializeXmlNode(xd,Newtonsoft.Json.Formatting.None, true);
return jsonString;
}
答案 0 :(得分:0)
如果不转换XML,我没有办法做到这一点。基本上,您需要将XML格式化为以下格式。
一种方法是将原始XML反序列化为模型A,将数据复制到模型B中,并将模型B序列化为JSON。
模型类:
<?xml version="1.0" encoding="UTF-8"?>
<manifest>
<packages>
<trackingNumber>23780498191986</trackingNumber>
<shipCost>14</shipCost>
<shipDate>2018-04-12T15:33:39.887</shipDate>
<items>
<dscoItemId>sdsd</dscoItemId>
<sku>sdsd</sku>
<quantity>1</quantity>
<lineNumber>1</lineNumber>
<originalLineNumber>1</originalLineNumber>
<originalOrderQuantity>1</originalOrderQuantity>
</items>
<items>
<dscoItemId>CCCVSD33</dscoItemId>
<sku>TESRRST123</sku>
<quantity>2</quantity>
<lineNumber>2</lineNumber>
<originalLineNumber>2</originalLineNumber>
<originalOrderQuantity>2</originalOrderQuantity>
</items>
<shipCarrier />
<shipMethod />
<shippingServiceLevelCode>FESDPO</shippingServiceLevelCode>
<numberOfLineItems>2</numberOfLineItems>
<shipFrom>
<attention>OPERATIONS m</attention>
<firstName>SD</firstName>
<lastName>SD</lastName>
<company />
<address1>ADD1</address1>
<address2 />
<city>URBANCREST</city>
<region>OH</region>
<postal>43123</postal>
<country>US</country>
<phone>123.125.1265</phone>
<email />
<locationCode />
</shipFrom>
<shipTo>
<attention>SD</attention>
<firstName>DD</firstName>
<lastName>DD</lastName>
<company>ABC CORPORATION</company>
<address1>123 FINE WAY</address1>
<address2>BUILDING F-1</address2>
<city>BALTIMORE</city>
<region>MD</region>
<postal>21234</postal>
<country>US</country>
<phone>222-222-2222</phone>
<email>XYZ@ACOMP.COM</email>
<locationCode />
</shipTo>
<shipWeight>35.00</shipWeight>
</packages>
</manifest>
目标XML:
implementation 'com.squareup.picasso:picasso:2.71828'