我正在嘗試將XML轉換爲JSON,以便爲API生成HTTP POST請求。我得到一個錯誤,因爲其中一個字段是一個整數而不是一個字符串。從我讀過的內容中,向節點添加「json:Integer =」true「」將導致它成爲一個int,但這似乎不適合我。這裏是xml和生成的json。數組正在工作,但整數不是。將XML轉換爲JSON時的C#強制整數
<shipments json:Array="true" xmlns:json="http://james.newtonking.com/projects/json">
<shipment_tracking_number />
<response_shipment_date>2016-10-18T01:00:00.0000000-04:00</response_shipment_date>
<response_shipment_method>UPS Ground</response_shipment_method>
<expected_delivery_date>2016-10-18T01:00:00.0000000-04:00</expected_delivery_date>
<ship_from_zip_code>12345</ship_from_zip_code>
<carrier_pick_up_date>2016-10-18T01:00:00.0000000-04:00</carrier_pick_up_date>
<carrier>UPS</carrier>
<shipment_items json:Array="true">
<shipment_item_id>FF12345K</shipment_item_id>
<alt_shipment_item_id>1234567890</alt_shipment_item_id>
<merchant_sku>B00xxxx</merchant_sku>
<response_shipment_sku_quantity json:Integer="true">1</response_shipment_sku_quantity>
</shipment_items>
</shipments>
串jsonrequest = JsonConvert.SerializeXmlNode(DOC, Newtonsoft.Json.Formatting.None,TRUE);
{"shipments":[
{
"shipment_tracking_number":null,
"response_shipment_date":"2016-10-18T01:00:00.0000000-04:00",
"response_shipment_method":"UPS Ground",
"expected_delivery_date":"2016-10-18T01:00:00.0000000-04:00",
"ship_from_zip_code":"12345",
"carrier_pick_up_date":"2016-10-18T01:00:00.0000000-04:00",
"carrier":"UPS",
"shipment_items":[
{
"shipment_item_id":"FF12345K",
"alt_shipment_item_id":"1234567890",
"merchant_sku":"B00xxxx",
"response_shipment_sku_quantity":"1"
}]
}]
}
我需要"response_shipment_sku_quantity":"1"
以顯示爲"response_shipment_sku_quantity":1
,但它似乎沒有奏效。我可以修改XML或執行轉換的代碼。我不介意只要能做到這一點。
您是否正在使用[將Json值與Newtonsoft整數轉換]中描述的'XmlNodeConverter'的「分叉」版本(https://stackoverflow.com/questions/29909328/convert-a-json-value-to-整數與-newtonsoft)? – dbc