1
"order_item" : [{"price":"250.00","product_code":"1","quantity":"2","
total":"500.00"},
{"price":"200.00","product_**code":"2","quantity":"1","**total":"200.00"} ]
}

I have to convert my hash map array list into above json array format.In my first position of array list i have sets like price=250.00,product_code=1,quantity=2,total:500.00,in my second position price=60.00,product_code=55,quantity=10,total:600.00 and so on.Any of them know answer help me.

3
  • 1
    Gson gson = new Gson(); String json = gson.toJson(YourHashMaphere); System.out.println("json = " + json); Go to this link for More information :Guide To JSON . it might help you Commented Jan 10, 2013 at 13:10
  • This is my array list values : [{item_quantity=2, item_nmae=Chargrilled vegetable salad, Product_id=17, item_price=100, Total=200, avaliable_quantity=12}, {item_quantity=1, item_nmae=Mushroom Soup, Product_id=12, item_price=120, Total=120, avaliable_quantity=28}] Commented Jan 10, 2013 at 13:56
  • and tryied like this for(int i=0;i<alist.size();i++) { alist_item.put("product_code",""+alist.get(i).get(PRODUCT_ID)); //Log.d("jSON", ""+alist.get(i).get(PRODUCT_ID)); alist_item.put("quantity", ""+alist.get(i).get(ITEM_QUANTITY_COLUMN)); alist_item.put("price",""+alist.get(i).get(ITEM_PRICE_COLUMN)); alist_item.put("total",""+alist.get(i).get(TOTAL_COLUMN)); jsonArray.put(alist_item); } Commented Jan 10, 2013 at 13:57

1 Answer 1

6
public class Product 
{
   private String price;

   private String product_code;

   private String quantity;

   private int total;

   public Product(String price, String product_code, String quantity, int total)
   {
      this.price = price;
      this.product_code = product_code;
      this.quantity = quantity;
      this.total = total;
   }
}  

business logic

new Gson().toJson(map);

usage

      Product pr1 = new Product("str1", "str1", "str1", 250);
      Product pr2 = new Product("str2", "str2", "str2", 200);
      List<Product> list = new ArrayList<Product>();
      list.add(pr1);
      list.add(pr2);
      Map<String, List<Product>> map = new HashMap<String, List<Product>>();
      map.put("order_item", list);

      System.out.println(new Gson().toJson(map));  

EDIT (using json.org)

      Map<String, String> jsonMap1 = new HashMap<String, String>();
      jsonMap1.put("price", "250.00");
      jsonMap1.put("product_code", "1");
      jsonMap1.put("quantity", "2");
      jsonMap1.put("total", "200");
      Map<String, String> jsonMap2 = new HashMap<String, String>();
      jsonMap2.put("price", "250.00");
      jsonMap2.put("product_code", "1");
      jsonMap2.put("quantity", "2");
      jsonMap2.put("total", "200");

      JSONObject json1 = new JSONObject(jsonMap1);
      JSONObject json2 = new JSONObject(jsonMap2);

      JSONArray array = new JSONArray();
      array.put(json1);
      array.put(json2);

      JSONObject finalObject = new JSONObject();
      finalObject.put("order_item", array);

      System.out.println(finalObject.toString());  

EDIT2 (using Gson)

      Map<String, String> jsonMap1 = new HashMap<String, String>();
      jsonMap1.put("price", "250.00");
      jsonMap1.put("product_code", "1");
      jsonMap1.put("quantity", "2");
      jsonMap1.put("total", "200");
      Map<String, String> jsonMap2 = new HashMap<String, String>();
      jsonMap2.put("price", "250.00");
      jsonMap2.put("product_code", "1");
      jsonMap2.put("quantity", "2");
      jsonMap2.put("total", "200");
      List<Map<String, String>> list = new ArrayList<Map<String, String>>();
      list.add(jsonMap1);
      list.add(jsonMap2);
      Map<String, List<Map<String, String>>> map = new HashMap<String, List<Map<String, String>>>();
      map.put("order_item", list);

      System.out.println(new Gson().toJson(map));
Sign up to request clarification or add additional context in comments.

9 Comments

in side my array list like this price=250.00,product_code=1,quantity=2,total:500.00
@yugesh what type has this? Is it String?
ashMap<String, String> map = new HashMap<String, String>(); map.put(PRODUCT_ID, ""+Submenu_id); map.put(ITEM_AVALIABLE_QUANTITY, ""+item_quantity); map.put(ITEM_NAME_COLUMN,""+item_name); map.put(ITEM_PRICE_COLUMN,"" + item_price); map.put(ITEM_QUANTITY_COLUMN, "" + quantity); map.put(TOTAL_COLUMN, "" + Total); alist.add(map);
This is my array list values llya : [{item_quantity=2, item_nmae=Chargrilled vegetable salad, Product_id=17, item_price=100, Total=200, avaliable_quantity=12}, {item_quantity=1, item_nmae=Mushroom Soup, Product_id=12, item_price=120, Total=120, avaliable_quantity=28}]
k i tried and say.i got one thing to convert array list using gson its just automatically convert the array list to json array list llya
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.