我的输入Json格式是:
{
"customerId": "amit@gmail.com",
"totalAmount": "120",
"orderJson": [
{
"packageId": 3,
"variantcomponentsJson": [
{
"itemId": "3",
"itemQuantity": "Half"
},
{
"itemId": "4",
"itemQuantity": "2"
}
],
"optionalcomponentsJson": {
"itemPrice": "50",
"itemId": "5",
"itemQuantity": "2"
},
"quantity": 2
}
], "deliveryAddress": {
"id": -1,
"addressName": "my address",
"localityID": 1,
"localityName": "cvraman",
"areaID": 2,
"areaName": "kr puram",
"address": "kr puram railway",
"landMark": "bridge"
}
}
我使用Volley Library进行解析:而我的Java代码就像
public static void submitOrder(final PlaceOrderData placeOrderData, final String authToken, final PlaceOrderCallback callback) {
Log.e("log_tag", "Auth token " + authToken);
JSONObject jObjPlaceOrder = new JSONObject();
try {
jObjPlaceOrder.put("customerId", placeOrderData.getCustomerId());
jObjPlaceOrder.put("totalAmount", placeOrderData.getTotalAmount());
JSONArray jArrayOrderJson = new JSONArray();
for (OrderJson orderJson : placeOrderData.getOrderJson()) {
JSONObject jOBJOrder = new JSONObject();
jOBJOrder.put("packageId", orderJson.getPackageId());
JSONArray jArrayVariantComponentsJson = new JSONArray();
for (VariantcomponentsJson variantcomponentsJson : orderJson.getVariantcomponentsJson()) {
JSONObject jOBJVariantcomponentsJson = new JSONObject();
jOBJVariantcomponentsJson.put("itemId", variantcomponentsJson.getItemId());
jOBJVariantcomponentsJson.put("itemQuantity", variantcomponentsJson.getItemQuantity());
jArrayVariantComponentsJson.put(jOBJVariantcomponentsJson);
}
OptionalcomponentsJson optionalcomponentsJson = orderJson.getOptionalcomponentsJson();
JSONObject jOBJOptionalComponentsJson = new JSONObject();
jOBJOptionalComponentsJson.put("itemPrice", optionalcomponentsJson.getItemPrice());
jOBJOptionalComponentsJson.put("itemId", optionalcomponentsJson.getItemId());
jOBJOptionalComponentsJson.put("itemQuantity", optionalcomponentsJson.getItemQuantity());
jOBJOrder.put("variantcomponentsJson", jArrayVariantComponentsJson);
jOBJOrder.put("optionalcomponentsJson", jOBJOptionalComponentsJson);
jOBJOrder.put("quantity", orderJson.getQuantity());
jArrayOrderJson.put(jOBJOrder);
}
jObjPlaceOrder.put("orderJson", jArrayOrderJson);
DeliveryAddress deliveryAddress = placeOrderData.getDeliveryAddress();
JSONObject jOBJDeliveryAddress = new JSONObject();
jOBJDeliveryAddress.put("id", deliveryAddress.getId());
jOBJDeliveryAddress.put("addressName", deliveryAddress.getAddress());
jOBJDeliveryAddress.put("localityID", deliveryAddress.getLocalityID());
jOBJDeliveryAddress.put("localityName", deliveryAddress.getLocalityName());
jOBJDeliveryAddress.put("areaID", deliveryAddress.getAreaID());
jOBJDeliveryAddress.put("areaName", deliveryAddress.getAreaName());
jOBJDeliveryAddress.put("address", deliveryAddress.getAddress());
jOBJDeliveryAddress.put("landMark", deliveryAddress.getLandMark());
jObjPlaceOrder.put("deliveryAddress", jOBJDeliveryAddress);
} catch (JSONException e) {
e.printStackTrace();
}
RequestQueue requestQueue = VolleyRequestProcessor.getInstance().getRequestQueue();
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, ApiUrls.URL_POST_PLACE_ORDER, jObjPlaceOrder,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.e("log_tag", "response" + response.toString());
}
}
, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
NetworkResponse networkResponse = error.networkResponse;
ErrorInfo errorInfo;
if (networkResponse == null) {
errorInfo = new ErrorInfo("Unable to reach our servers. Make sure your internet connection works.");
} else {
errorInfo = new ErrorInfo(networkResponse.statusCode, "Internal error occurred while communicating with our servers.");
}
callback.onFailed(errorInfo);
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<>();
headers.put(HeaderFields.FIELD_AUTH_TOKEN, authToken);
return headers;
}
};
requestQueue.add(jsonObjectRequest);
}