2015-03-18 2 views
0

Im новичок в использовании JSONАнализировать JSON объект из URL

{ 
    "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 
} 

«Im сделан разбором массива orders.

Теперь я хочу разобрать o_total и поместить его в TextView. Любая идея, как это сделать?

Это, как я сделал в первом массиве = «Заказы» который отображается на ListView

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»

+1

http://stackoverflow.com/questions/9605913/how-to-parse-json-in-android – steven

+1

Это звучит, как вы знаете, как разобрать JSON, как вы что вы проанализировали массив заказов. Где вы сталкиваетесь с трудностями с общей суммой? –

+0

У меня проблемы с coz o_total находится вне квадратных скобок, я думаю. –

ответ

0

Я полагаю, вы сделали что-то как это получить массив:

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

Просто делать то же самое, чтобы ваш "o_total":

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

: '(Logcat Error' no value for o_total ' –

+0

Можете ли вы разместить свой код? – vinitius

+0

done ................ :) –

0

Вобще:

int o_total = jsonobject.get("o_total").getAsInt(); 
Смежные вопросы