编码JSON(android)

编码JSON(android),android,json,Android,Json,我试图从sqlite数据库中的数据解析json 我想要的输出: { "report":{ "product":[ { "size": "100" }, { "size": "200" } ] } } 我得到的输出: { "report":{ "product":[ { "size": "200" }, { "size": "200" } ] } } 我的代码: Data = new JSO

我试图从sqlite数据库中的数据解析json

我想要的输出:

{
 "report":{
  "product":[
   {
    "size": "100"
   },
   {
    "size": "200"
   }
  ]
 }
}
我得到的输出:

{
 "report":{
  "product":[
   {
    "size": "200"
   },
   {
    "size": "200"
   }
  ]
 }
}
我的代码:

Data = new JSONObject();
Product = new JSONObject();
Report = new JSONObject();
SizeList = controller.distinctSize();       
for (HashMap<String, String> map : SizeList){
    for(Entry<String, String> mapEntry : map.entrySet()){
    String key = mapEntry.getKey();
    Object value = mapEntry.getValue();
    Report.accumulate("product", Product);
    Product.put(key, value); //size
    }
}
Data.put("report", Report);
try{
            Data = new JSONObject();
            Report = new JSONObject();

            Date date = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 
            int i=0;
            SizeList = controller.distinctSize();       
            for (HashMap<String, String> map : SizeList){
                for(Entry<String, String> mapEntry : map.entrySet()){
                    String key = mapEntry.getKey();
                    Object value = mapEntry.getValue();
                    Product = new JSONObject();

                    Product.put(key, value); //size
                    ComList = controller.distinctCommodity((String) value);
                    for (HashMap<String, String> map1 : ComList){
                        for(Entry<String, String> mapEntry1 : map1.entrySet()){
                            i++;
                            String key1 = mapEntry1.getKey();
                            Object value1 = mapEntry1.getValue();
                            if(i>1){
                                Product = new JSONObject();
                                Report.accumulate("product", Product);
                                Product.put(key1, value1); //commodity
                                Product.put(key, value);
                            }
                            else{
                                Report.accumulate("product", Product);
                                Product.put(key1, value1); //commodity
                            }
                            EstabList = controller.establishment( (String) value, (String) value1);
                            for (HashMap<String, String> map2 : EstabList){ 
                                for(Entry<String, String> mapEntry2 : map2.entrySet()){
                                    String key2 = mapEntry2.getKey();
                                    Object value2 = mapEntry2.getValue();
                                    PriceList = controller.price((String) value, (String) value1, (String) value2);
                                    for (HashMap<String, String> map3 : PriceList){ 
                                        Price = new JSONObject();
                                        Product.put("prices", Price);
                                        for(Entry<String, String> mapEntry3 : map3.entrySet()){
                                            String key3 = mapEntry3.getKey();
                                            Object value3 = mapEntry3.getValue();
                                            Price.put((String) value2, value3); //estabid and price

                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            Report.accumulate("product",Product);
            Report.put("report-date", dateFormat.format(date));
            DateList = controller.dates();
            for (HashMap<String, String> map : DateList){   
                for(Entry<String, String> mapEntry : map.entrySet()){
                    String key = mapEntry.getKey();
                    Object value = mapEntry.getValue();
                    Report.accumulate(key, value);
                }
            }
            Data.put("report", Report);
但我从代码中得到的是:

{
"report":{
    "report-date":"19-03-2013",
    "period-start":"18-03-2013",
    "period-end":"22-03-2013",
    "products":[
        {
            "commodity":"123456789",
            "size":"155g",
            "prices":{
                "est_id_1":"12.60",
                //missing one establishment here
            }
        },
        {
            "commodity":"234567890",
            "size":"155g",
            "prices":{
                "est_id_1":"3.5",
            }
        }
    ]
}
}
我的代码:

Data = new JSONObject();
Product = new JSONObject();
Report = new JSONObject();
SizeList = controller.distinctSize();       
for (HashMap<String, String> map : SizeList){
    for(Entry<String, String> mapEntry : map.entrySet()){
    String key = mapEntry.getKey();
    Object value = mapEntry.getValue();
    Report.accumulate("product", Product);
    Product.put(key, value); //size
    }
}
Data.put("report", Report);
try{
            Data = new JSONObject();
            Report = new JSONObject();

            Date date = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 
            int i=0;
            SizeList = controller.distinctSize();       
            for (HashMap<String, String> map : SizeList){
                for(Entry<String, String> mapEntry : map.entrySet()){
                    String key = mapEntry.getKey();
                    Object value = mapEntry.getValue();
                    Product = new JSONObject();

                    Product.put(key, value); //size
                    ComList = controller.distinctCommodity((String) value);
                    for (HashMap<String, String> map1 : ComList){
                        for(Entry<String, String> mapEntry1 : map1.entrySet()){
                            i++;
                            String key1 = mapEntry1.getKey();
                            Object value1 = mapEntry1.getValue();
                            if(i>1){
                                Product = new JSONObject();
                                Report.accumulate("product", Product);
                                Product.put(key1, value1); //commodity
                                Product.put(key, value);
                            }
                            else{
                                Report.accumulate("product", Product);
                                Product.put(key1, value1); //commodity
                            }
                            EstabList = controller.establishment( (String) value, (String) value1);
                            for (HashMap<String, String> map2 : EstabList){ 
                                for(Entry<String, String> mapEntry2 : map2.entrySet()){
                                    String key2 = mapEntry2.getKey();
                                    Object value2 = mapEntry2.getValue();
                                    PriceList = controller.price((String) value, (String) value1, (String) value2);
                                    for (HashMap<String, String> map3 : PriceList){ 
                                        Price = new JSONObject();
                                        Product.put("prices", Price);
                                        for(Entry<String, String> mapEntry3 : map3.entrySet()){
                                            String key3 = mapEntry3.getKey();
                                            Object value3 = mapEntry3.getValue();
                                            Price.put((String) value2, value3); //estabid and price

                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            Report.accumulate("product",Product);
            Report.put("report-date", dateFormat.format(date));
            DateList = controller.dates();
            for (HashMap<String, String> map : DateList){   
                for(Entry<String, String> mapEntry : map.entrySet()){
                    String key = mapEntry.getKey();
                    Object value = mapEntry.getValue();
                    Report.accumulate(key, value);
                }
            }
            Data.put("report", Report);
试试看{
数据=新的JSONObject();
报告=新的JSONObject();
日期=新日期();
SimpleDataFormat dateFormat=新SimpleDataFormat(“yyyy-MM-dd”);
int i=0;
SizeList=controller.distinctSize();
for(HashMap映射:SizeList){
对于(条目mapEntry:map.entrySet()){
String key=mapEntry.getKey();
对象值=mapEntry.getValue();
Product=新的JSONObject();
Product.put(键、值);//大小
ComList=控制器.distinctCommodity((字符串)值);
for(HashMap映射1:ComList){
对于(条目mapEntry1:map1.entrySet()){
i++;
字符串key1=mapEntry1.getKey();
对象值1=mapEntry1.getValue();
如果(i>1){
Product=新的JSONObject();
报告.累计(“产品”,产品);
Product.put(键1,值1);//商品
产品。投入(关键、价值);
}
否则{
报告.累计(“产品”,产品);
Product.put(键1,值1);//商品
}
EstabList=控制器。建立((字符串)值,(字符串)值1);
对于(HashMap2:EstabList){
对于(条目mapEntry2:map2.entrySet()){
字符串key2=mapEntry2.getKey();
对象值2=mapEntry2.getValue();
PriceList=controller.price((字符串)值,(字符串)值1,(字符串)值2);
对于(HashMap3:PriceList){
价格=新的JSONObject();
产品卖出价(“价格”,价格);
对于(条目mapEntry3:map3.entrySet()){
字符串key3=mapEntry3.getKey();
对象值3=mapEntry3.getValue();
Price.put((字符串)value2,value3);//estabid和Price
}
}
}
}
}
}
}
}
报告.累计(“产品”,产品);
Report.put(“报告日期”,dateFormat.format(日期));
DateList=controller.dates();
对于(HashMap-map:DateList){
对于(条目mapEntry:map.entrySet()){
String key=mapEntry.getKey();
对象值=mapEntry.getValue();
报告。累计(键、值);
}
}
数据。提交(“报告”,报告);
我应该怎么做?

Data=new JSONObject();
Data = new JSONObject(); 
Report = new JSONObject();
SizeList = controller.distinctSize();       
for (HashMap<String, String> map : SizeList){
    for(Entry<String, String> mapEntry : map.entrySet()){
    String key = mapEntry.getKey();
    Object value = mapEntry.getValue();
    Product = new JSONObject();
    Report.accumulate("product", Product);

    Product.put(key, value); //size
    }
}
报告=新的JSONObject(); SizeList=controller.distinctSize(); for(HashMap映射:SizeList){ 对于(条目mapEntry:map.entrySet()){ String key=mapEntry.getKey(); 对象值=mapEntry.getValue(); Product=新的JSONObject(); 报告.累计(“产品”,产品); Product.put(键、值);//大小 } }
在循环中
产品
被重写。每次初始化

产品变量都不会在循环中重新初始化。您应该为for循环的每次迭代初始化产品。

您好,我可以问您更多关于这个问题的问题吗?已经更新。请查看我的问题。抱歉,我真的不知道如何做。