我试图解析仍然无法做到的数据。
String jsonData = "[{\"id\":\"7\",\"customer_id\":\"1\",\"product_type_id\":\"1\",\"quantity\":\"1\",\"ordered_on\":\"2015-12-01\",\"product_consumption_type\":\"general\",\"reason\":\"\",\"productType\":{\"id\":\"1\",\"name\":\"Full Cream Milk\",\"description\":\"\",\"measurement\":\"\"}},{\"id\":\"3\",\"customer_id\":\"1\",\"product_type_id\":\"1\",\"quantity\":\"0\",\"ordered_on\":\"2015-12-02\",\"product_consumption_type\":\"general\",\"reason\":\"\",\"productType\":{\"id\":\"1\",\"name\":\"Full Cream Milk\",\"description\":\"\",\"measurement\":\"\"}}]";
这是我迄今为止所尝试过的:
try {
JsonArrayRequest req = new JsonArrayRequest(jsonData, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
try {
//parsing the value of jsaon array
for (int i = 0; i < response.length(); i++) {
JSONObject details = (JSONObject) response.get(i);
String da = details.getString("ordered_on");
int qty = Integer.parseInt(details.getString("quantity"));
JSONObject prod_det = details.getJSONObject("productType");
String na = prod_det.getString("name");
ProductType pt = new ProductType(na, qty);
Product prod = new Product(da, pt);
list.add(prod);
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(MonthlySummaryActivity.this, "ERROR" + e.getMessage(), Toast.LENGTH_SHORT).show();
adapter.notifyDataSetChanged();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MonthlySummaryActivity.this, "error::" + error.getMessage(), Toast.LENGTH_LONG).show();
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
loading.hide();
return list;
答案 0 :(得分:2)
您可以通过两种方式解析JSON数据。
1)创建自己的解析器类。
2)使用库来解析json字符串。
1)如果json字符串很短,那么您应该使用自己的解析器类,如下所示。
String name;
try {
JSONArray array = new JSONArray(YOUR JSON STRING);
JSONObject obj;
for (int i = 0; i < array.length(); i++) {
obj = array.getJSONObject(i);
name = obj.getString(KEY_NAME);
......
}
}catch (Exception e){
e.printStackTrace();
}
2)使用Gson库。对于Gson,您可以使用this。
答案 1 :(得分:0)
希望我能帮助你。
public List<Product> parse(String jsonData) throws JSONException {
List<Product> productList = new ArrayList();
JSONArray orderArray = new JSONArray(jsonData);
for (int i = 0, count = orderArray.length(); i < count; i++) {
JSONObject productJO = orderArray.getJSONObject(i);
ProductType productType = new ProductType();
// parse the product type
JSONObject productTypeJO = productJO.getJSONObject("productType");
String typeId = productTypeJO.getString("id");
String name = productTypeJO.getString("name");
String description = productTypeJO.getString("description");
String measurement = productTypeJO.getString("measurement");
productType.setId(typeId);
productType.setName(name);
productType.setDescription(description);
productType.setMeasurement(measurement);
// parse the product
String id = productJO.getString("id");
String customerId = productJO.getString("customer_id");
String productTypeId = productJO.getString("product_type_id");
String quantity = productJO.getString("quantity");
String orderedOn = productJO.getString("ordered_on");
String productConsumptionType = productJO.getString("product_consumption_type");
String reason = productJO.getString("reason");
Product product = new Product();
product.setId(id);
product.setCustomerId(customerId);
product.setProductTypeId(productTypeId);
product.setQuantity(quantity);
product.setOrderedOn(orderedOn);
product.setProductConsumptionType(productConsumptionType);
product.setReason(reason);
product.setProductType(productType);
productList.add(product);
}
return productList;
}
答案 2 :(得分:0)
简单高效:使用Googlle的 Gson库
implementation 'com.google.code.gson:gson:2.6.2'
Type type = new TypeToken<Map<String, String>>(){}.getType();
Map<String, String> myMap = gson.fromJson(JsonString , type);
稍后谢谢:)