0

在我的应用程序中,我得到了这样的 J SON 响应

{
"orderDetails": [
    {
        "order_details": {
            "branch_code": 0,
            "cust_code": 87,
            "custom_instructions": "<null>",
            "merchant_code": "<null>",
            "order_number": 419,
            "order_type_code": 1,
            "packing_charge": 0,
            "price": 200,
            "product_code": 21,
            "quantity": 1,
            "value": 200
        },
        "order_headers": {
            "branch_code": 0,
            "cust_code": 87,
            "delivered_time": "<null>",
            "delivery_charge": 23,
            "delivery_date": "2013-05-02",
            "delivery_support": "",
            "delivery_time": "12:52:25",
            "grand_total": 354,
            "merchant_code": "<null>",
            "order_datetime": "2013-05-02 12:52:25",
            "order_status": "Pending",
            "order_type_code": 1,
            "order_value": 354,
            "packing_charge": 0,
            "payment_status": "Pending",
            "payment_type": "Cash",
            "sub_total": 325,
            "vat_percent": 7
        }
    },
    {
        "order_details": {
            "branch_code": 0,
            "cust_code": 87,
            "custom_instructions": "<null>",
            "merchant_code": "<null>",
            "order_number": 419,
            "order_type_code": 1,
            "packing_charge": 0,
            "price": 125,
            "product_code": 35,
            "quantity": 1,
            "value": 125
        },
        "order_headers": {
            "branch_code": 0,
            "cust_code": 87,
            "delivered_time": "<null>",
            "delivery_charge": 23,
            "delivery_date": "2013-05-02",
            "delivery_support": "",
            "delivery_time": "12:52:25",
            "grand_total": 354,
            "merchant_code": "<null>",
            "order_datetime": "2013-05-02 12:52:25",
            "order_status": "Pending",
            "order_type_code": 1,
            "order_value": 354,
            "packing_charge": 0,
            "payment_status": "Pending",
            "payment_type": "Cash",
            "sub_total": 325,
            "vat_percent": 7
        }
    }
],
"orderSuccess": 1
}

我想将所有 order_details 放在单个数组列表中,我不知道如何解决这个问题。有人知道请帮我解决这个问题。

4

5 回答 5

1
JSONArray j1;
    JSONObject ordeedetail,orderheader,mainobject,main;
    String custinstruction,brnchcode,custcode,deliverytime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            main = new JSONObject(jsonstring);
            Log.e(main.toString(),"string");
            j1 = main.getJSONArray("orderDetails");
            Log.e(j1.toString(),"string");
            for(int i=0;i<j1.length();i++)
            {
                mainobject= j1.getJSONObject(i);
                 if (mainobject.has("order_details")){
                     ordeedetail = mainobject.getJSONObject("order_details");   
                     brnchcode=ordeedetail.getString("branch_code").toString();
                     Log.e(brnchcode.toString(),"branch_code");
                     custcode=ordeedetail.getString("cust_code").toString();  
                     Log.e(custcode.toString(),"cust_code");
                     custinstruction=ordeedetail.getString("custom_instructions").toString(); 
                     Log.e(custinstruction.toString(),"custinstruction");
                 }
                 else if(mainobject.has("order_headers")){
                     orderheader = mainobject.getJSONObject("order_details");   
                     brnchcode=orderheader.getString("branch_code").toString();
                     Log.e(brnchcode.toString(),"branch_code");
                     custcode=orderheader.getString("cust_code").toString();  
                     Log.e(custcode.toString(),"cust_code");
                     deliverytime=orderheader.getString("delivered_time").toString(); 
                     Log.e(custinstruction.toString(),"deliverytime");
                 }
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
try this logic....
于 2013-05-02T13:22:20.077 回答
0

这是一个简单的问题,你可以自己解决。只需阅读Android中的 JSON - TutorialAndroid JSON Parsing Tutorial

尝试自己做所有事情,而不是复制/粘贴 StackOverflow。
问候

更新对不起,它必须是评论。

于 2013-05-02T11:37:19.427 回答
0

您可以使用 hashmaps 的数组列表来解决这个问题

ArrayList<HashMap<String,String>> List=new ArrayList<HashMap<String,String>>;
HashMap myHashMap=new HashMap<String, String>();
myHashMap.put(" ",values);
List.add(myHashMap);
于 2013-05-02T11:38:56.877 回答
0
// Convert String to json object
JSONObject json = new JSONObject(responseString);

// get order_details json object
JSONObject json_order_details = json.getJSONObject("order_details");

// get value from LL Json Object

myArrayList1.add(json_order_details.getString("branch_code"));
myArrayList2.add(json_order_details.getString("cust_code"));
myArrayList3.add(json_order_details.getString("order_number"));
myArrayList4.add(json_order_details.getString("price"));
.
. 
.
于 2013-05-02T11:42:18.607 回答
0

我没有检查代码,此代码帮助您需要添加更多内容作为参考

 public void parse(String data ){
    JsonObject j1 = new JsonObject(data);
    String orderSuccess = j1.getString("orderSuccess");

    if(j1.has("orderDetails"))
    JsonArray jArray = new JsonArray(j1.getString("orderDetails"));
    //jArray loop with size 
    for(i=0;i<jsonArray.length() ; i++){
    ArrayList<FeedObject> arrayList = new ArrayList<FeedObject>();
    FeedObject feedObject = new FeddObject ();
    JsonObject j2 = jArray.getJsonObject(i);

    if(j2.has("order_details")){
    JsonObject j3 = j2.getJsonObject("order_details");
    }

    if(j2.has("order_headers")){
    JsonObject j4 = j2.getJsonObject("order_headers");
    }

    feedObject.branch_code__order_details = j3.getString("branch_code");
    .
    .
    .
    .
    feedObject.branch_code__order_headers = j4.getString("branch_code");
    .
    .
    .
    .


    feedbject.orderSuccess  = orderSuccess ;

    }


    }
于 2013-05-02T12:41:30.843 回答