将JSON对象转换为JSON数组

时间:2018-09-28 10:25:03

标签: android json android-layout android-fragments

我正在研究将多种类型的数据组合为一种的功能。我已经将数据转换为JSON对象,但是问题是,在一个API中,我需要JSON Object格式,而在另一个API中,我需要JSON Array格式。因此,如何更改JSON数组格式的JSON对象数据。

我的数据格式如下:

       {  
      "id":"1",
      "name":"TEST-debiteur\/b120511",
      "owner_name":"null",
      "email":"null",
      "phone_number":"null",
      "street_name":"null",
      "postal_code":"null",
      "province":"null",
      "city":"null",
      "country":"null",
      "pre_order":{  
         "product":[  
            {  
               "id":"1",
               "name":"Showbord JéWé vloerprofielen 50cm presentatie, 500x400mm zelfklevend",
               "article_code":"00043",
               "ean":"8711283393001",
               "mbh":"1",
               "quantity":"8",
               "total_quantity":"8"
            },
            {  
               "id":"2",
               "name":"Showbord JéWé vloerprofielen 100cm presentatie, 1000x400mm zelfklevend",
               "article_code":"00044",
               "ean":"8711283393018",
               "mbh":"1",
               "quantity":"1",
               "total_quantity":"1"
            },
            {  
               "id":"4",
               "name":"Showtrap trapreno vinyl zilvergrijs eiken 2 treden 42x23x25cm",
               "article_code":"00077",
               "ean":"8711283408545",
               "mbh":"1",
               "quantity":"2",
               "total_quantity":"2"
            }
         ],
         "trading":[  

         ]
      },
      "unrecognised_product_list":[  

      ],
      "remarks_by_shop_owner":"222",
      "internal_remarks":""
   }

但是我需要以下格式:在开始和结束处添加[]

     [{  
      "id":"1",
      "name":"TEST-debiteur\/b120511",
      "owner_name":"null",
      "email":"null",
      "phone_number":"null",
      "street_name":"null",
      "postal_code":"null",
      "province":"null",
      "city":"null",
      "country":"null",
      "pre_order":{  
         "product":[  
            {  
               "id":"1",
               "name":"Showbord JéWé vloerprofielen 50cm presentatie, 500x400mm zelfklevend",
               "article_code":"00043",
               "ean":"8711283393001",
               "mbh":"1",
               "quantity":"8",
               "total_quantity":"8"
            },
            {  
               "id":"2",
               "name":"Showbord JéWé vloerprofielen 100cm presentatie, 1000x400mm zelfklevend",
               "article_code":"00044",
               "ean":"8711283393018",
               "mbh":"1",
               "quantity":"1",
               "total_quantity":"1"
            },
            {  
               "id":"4",
               "name":"Showtrap trapreno vinyl zilvergrijs eiken 2 treden 42x23x25cm",
               "article_code":"00077",
               "ean":"8711283408545",
               "mbh":"1",
               "quantity":"2",
               "total_quantity":"2"
            }
         ],
         "trading":[  

         ]
      },
      "unrecognised_product_list":[  

      ],
      "remarks_by_shop_owner":"222",
      "internal_remarks":""
   }]

以及将其转换为JSON对象的代码。

json_shops_order_list = new JSONObject();
//                ARRAY OF PRE-ORDER
            JSONObject pre_order = new JSONObject();
//                array of json product
            JSONArray jsonProductArray = new JSONArray();

            for (ModelCart modelCart : modelCartProductList) {

                id = modelCart.getCart_product_id();
                name = modelCart.getCart_product_name_nl();
                article_code = modelCart.getCart_product_art();
                ean = modelCart.getCart_product_EAN();
                mbh = modelCart.getCart_product_MBH();
                quantity = modelCart.getProduct_quantity();
                total_quantity = modelCart.getProduct_total_quantity();

                JSONObject json = new JSONObject();

                try {
                    json.put("id", id);
                    json.put("name", name);
                    json.put("article_code", article_code);
                    json.put("ean", ean);
                    json.put("mbh", mbh);
                    json.put("quantity", quantity);
                    json.put("total_quantity", total_quantity);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                jsonProductArray.put(json);

            }

            Log.e("TAG", "jsonProduct: " + jsonProductArray);


//               array of jsontrading

            JSONArray jsonTradingArray = new JSONArray();

            for (ModelCart modelCart : modelCartTradingList) {

                id = modelCart.getCart_product_id();
                name = modelCart.getCart_product_name_nl();
                article_code = modelCart.getCart_product_art();
                ean = modelCart.getCart_product_EAN();
                mbh = modelCart.getCart_product_MBH();
                quantity = modelCart.getProduct_quantity();
                total_quantity = modelCart.getProduct_total_quantity();

                JSONObject json = new JSONObject();

                try {
                    json.put("id", id);
                    json.put("name", name);
                    json.put("article_code", article_code);
                    json.put("ean", ean);
                    json.put("mbh", mbh);
                    json.put("quantity", quantity);
                    json.put("total_quantity", total_quantity);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                jsonTradingArray.put(json);

            }
            Log.e("TAG", "jsonTrading: " + jsonTradingArray);

            try {
                pre_order.put("product", jsonProductArray);
                pre_order.put("trading", jsonTradingArray);
            } catch (Exception e) {
                e.printStackTrace();
            }
            Log.e("TAG", "pre_order:" + pre_order);

//                ARRAY OF UNRECOGNISED CODE

            JSONArray unrecognised_product_list = new JSONArray();

            for (ModelUnrecognisedCode modelUnrecognisedCode : modelUnrecognisedCodesArrayList) {

                code = modelUnrecognisedCode.getUnrecognised_code();
                comment = modelUnrecognisedCode.getUnrecognised_comment();
                Log.e("TAG", "modelUnrecognisedCodesArrayList:log " + code);
                JSONObject json = new JSONObject();

                try {
                    json.put("code", code);
                    json.put("comment", comment);

                } catch (JSONException e) {
                    e.printStackTrace();
                }

                unrecognised_product_list.put(json);

            }

            Log.e("TAG", "unrecognised_product_list: " + unrecognised_product_list);

            SessionManager sessionManager = new SessionManager(getActivity());
            HashMap<String, String> shopDetails = sessionManager.getSelectedShopDetail();


            Log.e("TAG", "selectedshopdetails: " + shopDetails.get("shop_email") + " " + shopDetails.get("street_name") + " " +
                    shopDetails.get("postal_code") + " " + shopDetails.get("province") + " " + shopDetails.get("city") + " " + shopDetails.get("country"));


            String email = shopDetails.get("shop_email");
            String street = shopDetails.get("street_name");
            String postal_code = shopDetails.get("postal_code");
            String province = shopDetails.get("province");
            String city = shopDetails.get("city");
            String country = shopDetails.get("country");

            if (street != null) {
                shop_street_name = shopDetails.get("street_name");
                Log.e("TAG", "shop_street" + "" + shop_street_name);
            } else {
                shop_street_name = "null";
            }
            if (email != null) {
                shop_email = shopDetails.get("shop_email");
                Log.e("TAG", "shop_email" + "" + shop_email);
            } else {
                shop_email = "null";
            }
            if (postal_code != null) {
                shop_postal_code = shopDetails.get("postal_code");
                Log.e("TAG", "shop_postal" + "" + shop_postal_code);
            } else {
                shop_postal_code = "null";
            }
            if (province != null) {
                shop_province = shopDetails.get("province");
                Log.e("TAG", "shop_province" + "" + shop_province);
            } else {
                shop_province = "null";
            }
            if (city != null) {
                shop_city = shopDetails.get("city");
                Log.e("TAG", "shop_city" + "" + shop_city);
            } else {
                shop_city = "null";
            }
            if (country != null) {
                shop_country = shopDetails.get("country");
                Log.e("TAG", "shop_country" + "" + shop_country);
            } else {
                shop_country = "null";
            }

            //   JSONObject json = new JSONObject();

            try {
                json_shops_order_list.put("id", shopDetails.get("shop_id"));
                json_shops_order_list.put("name", shopDetails.get("shop_name"));
                json_shops_order_list.put("owner_name", shopDetails.get("owner_name"));
                json_shops_order_list.put("email", shop_email);
                json_shops_order_list.put("phone_number", shopDetails.get("phone"));
                json_shops_order_list.put("street_name", shop_street_name);
                json_shops_order_list.put("postal_code", shop_postal_code);
                json_shops_order_list.put("province", shop_province);
                json_shops_order_list.put("city", shop_city);
                json_shops_order_list.put("country", shop_country);
                json_shops_order_list.put("pre_order", pre_order);
                json_shops_order_list.put("unrecognised_product_list", unrecognised_product_list);
                json_shops_order_list.put("remarks_by_shop_owner", etRemarksShopOwner.getText().toString());
                json_shops_order_list.put("internal_remarks", etInternalRemarks.getText().toString());
            } catch (JSONException e) {
                e.printStackTrace();
            }

            Log.e("TAG", "shops_order_list: " + json_shops_order_list);

1 个答案:

答案 0 :(得分:0)

您只需要将您的对象添加到JSONArray中。试试这个:

JSONArray array = new JSONArray();
JSONObject object = //your object
array.put(object);