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.

Yugesh
  • 4,030
  • 9
  • 57
  • 97
  • 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](http://code.google.com/p/google-gson/) . it might help you – Nirav Tukadiya Jan 10 '13 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}] – Yugesh Jan 10 '13 at 13:56
  • and tryied like this for(int i=0;i – Yugesh Jan 10 '13 at 13:57

1 Answers1

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));
Ilya
  • 29,135
  • 19
  • 110
  • 158
  • in side my array list like this price=250.00,product_code=1,quantity=2,total:500.00 – Yugesh Jan 10 '13 at 13:29
  • @yugesh what type has *this*? Is it *String*? – Ilya Jan 10 '13 at 13:31
  • ashMap map = new HashMap(); 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); – Yugesh Jan 10 '13 at 13:48
  • 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}] – Yugesh Jan 10 '13 at 13:52
  • 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 – Yugesh Jan 10 '13 at 14:42
  • llya thank u so much i got the answer.u save my time.once again thank u llya,,,:) – Yugesh Jan 10 '13 at 15:38
  • @Ilya thanks for best tutorial just missing is Gson jar:( I downloaded and added now problem is solved :) +1:) – Bhavin Chauhan Jul 12 '13 at 07:52
  • @Ilya did you try with gson my order is changing again. Can you help me http://stackoverflow.com/questions/23429017/json-object-item-order – Sahin Yanlık May 02 '14 at 13:51
  • @llya do you know how to achieve the same with jackson or moxy..can you help me – Anudeep Samaiya Sep 02 '15 at 21:43