从URL解析JSON对象

时间:2015-03-18 20:46:19

标签: android json

我是新手使用JSON

{
  "orders": [
    {
      "id": "12",
      "ord_name": "Passion Fruit",
      "ord_desc": "Cold passion fruit",
      "ord_price": "75",
      "ord_qty": "3",
      "customer_id": "54feec24bff73",
      "ord_num": "13191554feec24bff73",
      "price_x_quan": "225.00",
      "image": "http:\/\/192.168.43.52\/MMOS\/uploads\/passion-fruit.jpg",
      "subtotal": "",
      "imgimg": "uploads\/passion-fruit.jpg"
    },
    {
      "id": "13",
      "ord_name": "Caramel Mocha",
      "ord_desc": "Cold caramel mocha",
      "ord_price": "100",
      "ord_qty": "1",
      "customer_id": "54feec24bff73",
      "ord_num": "13191554feec24bff73",
      "price_x_quan": "100.00",
      "image": "http:\/\/192.168.43.52\/MMOS\/uploads\/caramelmocha.jpg",
      "subtotal": "",
      "imgimg": "uploads\/caramelmocha.jpg"
    }
  ],
  "o_total": 325
}

'我完成了解析数组orders

我现在想要的是解析o_total并将其放在TextView上。 知道怎么做吗?

这就是我在第一个数组中做的''订单' 它显示在列表视图

try {
                // Locate the array name in JSON==
                jsonarray = jsonobject.getJSONArray("orders");


            for (int i = 0; i < jsonarray.length(); i++) {
                    HashMap<String, String> map = new HashMap<String, String>();
                    jsonobject = jsonarray.getJSONObject(i);

                    // Retrive JSON Objects

                    map.put("id", jsonobject.getString("id"));
                    map.put("ord_name", jsonobject.getString("ord_name"));
                    map.put("ord_price", jsonobject.getString("ord_price"));
                    map.put("ord_qty", jsonobject.getString("ord_qty"));
                    map.put("customer_id", jsonobject.getString("customer_id"));
                    map.put("ord_num", jsonobject.getString("ord_num"));
                    map.put("price_x_quan", jsonobject.getString("price_x_quan"));
                    map.put("image", jsonobject.getString("image"));
                    map.put("text_ordernumber",home_in.getStringExtra("text_ordernumber"));
                    map.put("text_name",home_in.getStringExtra("text_name"));

                    // Set the JSON Objects into the array
                    arraylist.add(map);


                }


            } catch (JSONException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }

我想知道如何在“o_total”

上做到这一点

2 个答案:

答案 0 :(得分:0)

我想你做了这样的事情来获得你的阵列:

 JSONObject rootObject = new JSONObject(yourJSONString);
 rootObject.getJSONArray("order");

对你的“o_total”做同样的事情:

 yourTextView.setText(rootObject.getString("o_total")); 

答案 1 :(得分:0)

只是做:

int o_total = jsonobject.get("o_total").getAsInt();