-1

我正在尝试从我的 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);

你能告诉我如何修复我的代码,以便我可以得到我想要的数据吗?

[[解决了]]

我有另一个问题。由于现在代码循环,我很难解析它。

我现在想要的输出:

{
"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",
                "est_id_3":"13.45",
            }
        },
        {
            "commodity":"234567890",
            "size":"155g",
            "prices":{
                "est_id_1":"3.5",
            }
        }
    ]
}
}

但我从我的代码中得到的是:

{
"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",
            }
        }
    ]
}
}

我的代码:

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

我该怎么做?

4

3 回答 3

1
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
    }
}
于 2013-05-14T07:09:04.177 回答
1

在循环product中被重写。每次初始化

于 2013-05-14T07:11:15.253 回答
1

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

于 2013-05-14T07:17:21.697 回答