我需要在JSON有效负载下实现对包括JSONArray和JSONObject的java对象的
我尝试使用下面的Java代码来实现相同的
DWYT_productOrderResponse CreateProductOrderResponse = new DWYT_productOrderResponse();
ResultHeader rslthdr = new ResultHeader();
JSONObject productOrder = new JSONObject();
JSONArray orderIt = new JSONArray();
JSONObject produt = new JSONObject();
// List<ProductCharacteristic> ProductChara = new ArrayList<ProductCharacteristic>();
Map ProductChara = new LinkedHashMap();
//Map orderItem = new LinkedHashMap();
// OrderIteam[] orderItem;
if (1 == 1) {
String output = null;
try {
productOrder.put("externalId", CreateOrderReq.getProductOrder().getExternalId());
productOrder.put("description", CreateOrderReq.getProductOrder().getDescription());
ProductChara.put("name", "CustomerName");
ProductChara.put("value", CreateOrderReq.getProductOrder().getOrderItem().getProduct().getProductCharacteristic().getCustomerName());
ProductChara.put("name", "CustomerContactNumber");
ProductChara.put("value", CreateOrderReq.getProductOrder().getOrderItem().getProduct().getProductCharacteristic().getCustomerContactNumber());
ProductChara.put("name", "CRMAddress");
ProductChara.put("value", CreateOrderReq.getProductOrder().getOrderItem().getProduct().getProductCharacteristic().getCRMAddress());
ProductChara.put("name", "CustomerEmail");
ProductChara.put("value", CreateOrderReq.getProductOrder().getOrderItem().getProduct().getProductCharacteristic().getCustomerEmail());
ProductChara.put("name", "CustomerGovetID");
ProductChara.put("value", CreateOrderReq.getProductOrder().getOrderItem().getProduct().getProductCharacteristic().getCustomerGovtID());
ProductChara.put("name", "ODBNo");
ProductChara.put("value", CreateOrderReq.getProductOrder().getOrderItem().getProduct().getProductCharacteristic().getODBNO());
produt.put("productCharacteristic", ProductChara);
produt.put("product", produt);
orderIt.put(produt);
productOrder.put("orderItem", orderIt);
productOrder.put("productOrder", productOrder);
} catch (JSONException e) {
e.printStackTrace();
}
}
这是上面代码的json有效负载的结果
{
"externalId":"CRM000000912",
"description":"Activation Request",
"orderItem":[
{
"productCharacteristic":{
"name":"CustomerName",
"value":"xxxx",
"name":"CustomerContactNumber",
"value":"5600000232",
"name":"CRMAddress",
"value":"xxxxxxx",
"name":"CustomerEmail",
"value":"xxx@xx.xxx",
"name":"CustomerGovetID",
"value":"1223232323232322",
"name":"ODBNo",
"value":"RYH-736834-JKS"
}
}
]
}
这是我要解析的json负载
{
"externalId": " 12345678",
"orderItem": [{
"product": {
"productCharacteristic": [{
"name": "CustomerName",
"value": "someone"
}, {
"name":
"CustomerContactNumber",
"value": "13524687502"
}, {
"name": "CRMAddress",
"value": "xxxxxx"
}, {
"name": "CustomerEmail ",
"value": "XXX"
}, {
"name": " CustomerGovet.ID",
"value": "XXX"
}, {
"name": " ODBNo.",
"value": "XXX"
}
]
}
}
]
}
是否有任何简单的方法或Java代码可以帮助我获得预期的输出。
答案 0 :(得分:0)
已解决:现在,在对我的代码进行了一些增强之后,我得到了预期的json负载
try {
productOrder.put("externalId", CreateOrderReq.getProductOrder().getExternalId());
productOrder.put("description", CreateOrderReq.getProductOrder().getDescription());
ProductChara.put("name","CustomerName");
ProductChara.put("value",CreateOrderReq.getProductOrder().getOrderItem().getProduct().getProductCharacteristic().getCustomerName());
ProductCharaArray.put(ProductChara);
ProductChara.put("name","CustomerContactNumber");
ProductChara.put("value",CreateOrderReq.getProductOrder().getOrderItem().getProduct().getProductCharacteristic().getCustomerContactNumber());
ProductCharaArray.put(ProductChara);
ProductChara.put("name","CRMAddress");
ProductChara.put("value",CreateOrderReq.getProductOrder().getOrderItem().getProduct().getProductCharacteristic().getCRMAddress());
ProductCharaArray.put(ProductChara);
ProductChara.put("name","CustomerEmail");
ProductChara.put("value",CreateOrderReq.getProductOrder().getOrderItem().getProduct().getProductCharacteristic().getCustomerEmail());
ProductCharaArray.put(ProductChara);
ProductChara.put("name","CustomerGovetID");
ProductChara.put("value",CreateOrderReq.getProductOrder().getOrderItem().getProduct().getProductCharacteristic().getCustomerGovtID());
ProductCharaArray.put(ProductChara);
ProductChara.put("name","ODBNo");
ProductChara.put("value",CreateOrderReq.getProductOrder().getOrderItem().getProduct().getProductCharacteristic().getODBNO());
ProductCharaArray.put(ProductChara);
produt.put("productCharacteristic",ProductCharaArray);
orderItm.put("product",produt);
orderItemArray.put(orderItm);
productOrder.put("orderItem", orderItemArray);
System.out.println(productOrder.toString());
} catch (JSONException e) {
e.printStackTrace();
}