0

I am developing a food booking app.The app has a cart which lists carted items in a list view. How to get all values from cart list view to JSON format.Below is a sample format,my data should be like this.

{
    "customer_id":"12",
    "product_details": [
                       {"proid":"1", "proname":"abc"},
                       {"proid":"1", "proname":"abc"},
                       {"proid":"3", "proname":"hh"},
                       {"proid":"4", "proname":"gg"}
                   ]
}
Harsh Sharma
  • 97
  • 1
  • 10

4 Answers4

1

Add following dependency

compile 'com.google.code.gson:gson:2.8.1'

And then use

Gson gson = new Gson(); 
gson.toJson(data)
Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
meuser07
  • 368
  • 4
  • 10
1

// try this code

    JSONObject input      = new JSONObject();
                JSONArray array       = new JSONArray();

                for (int i = 0; i < alSets.size(); i++)
                {
                    JSONObject obj_set1 = new JSONObject();                    
                    String setNo= "Set " + String.valueOf(i+1);
                    obj_set1.put("SetId", i + 1);
                    array.put(obj_set1);
                }
                input("Key", input);

JsonObjectRequest request = new JsonObjectRequest(requestURL,input, stringListener, errorListener);

        request.setRetryPolicy(new DefaultRetryPolicy(
                30000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

        addToRequestQueue(request);
Mohit Kalia
  • 351
  • 1
  • 6
0

A JSON serializing library like Gson is good for this. Simply annotate your model class:

@SerializedName("customer_id") private int customerId;
@SerializedName("product_details") private List<ProductDetails> productDetails;

// etc
terencey
  • 3,282
  • 4
  • 32
  • 40
0

Used GSON read the documentation.

MJ_
  • 63
  • 1
  • 1
  • 10