2013-01-10 92 views
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"} ] 
} 

我必须将我的哈希映射数组列表转换为上面的json数组格式。在我的第一个数组列表中,我有第二个数组,例如price = 250.00,product_code = 1,quantity = 2,total:500.00,在我的第二个仓位价格= 60.00,product_code = 55,数量= 10,总数:600.00等等。他们都知道答案帮助我。在android中哈希映射数组列表到JSON数组?

+1

GSON GSON =新GSON();字符串json = gson.toJson(YourHashMaphere); System.out.println(“json =”+ json);转到此链接以获取更多信息:[指向JSON](http://code.google.com/p/google-gson/)。它可能会帮助你 –

+0

这是我的数组列表值:{item_quantity = 2,item_nmae = Chargrilled蔬菜沙拉,Product_id = 17,item_price = 100,Total = 200,avaliable_quantity = 12},{item_quantity = 1,item_nmae =蘑菇汤,PRODUCT_ID = 12,ITEM_PRICE = 120,总= 120,avaliable_quantity = 28}] – Yugesh

+0

和tryied像这样(INT I = 0; I Yugesh

回答

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; 
    } 
} 

业务逻辑

new Gson().toJson(map); 

用法

 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(使用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(使用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)); 
+0

我的数组列表,这样的价格= 250.00,PRODUCT_CODE = 1,数量= 2,总:500.00 – Yugesh

+0

@yugesh什么类型有* *此?它是*弦* *? – Ilya

+0

ashMap <字符串,字符串>地图=新的HashMap <字符串,字符串>();\t \t \t \t \t map.put(PRODUCT_ID,“”+ Submenu_id); \t \t \t \t \t \t map.put(ITEM_AVALIABLE_QUANTITY “” + item_quantity); \t \t \t \t \t \t map.put(ITEM_NAME_COLUMN,“”+ item_name); \t \t \t \t \t \t map.put(ITEM_PRICE_COLUMN,“”+ item_price); \t \t \t \t \t \t地图。放(ITEM_QUANTITY_COLUMN,“”+数量); \t \t \t \t \t \t map.put(TOTAL_COLUMN,“”+ Total); \t alist.add(map); – Yugesh