2013-05-14 59 views
-1

我想從我的sqlite數據庫中的數據解析json。編碼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); 

我應該怎麼做呢?

回答

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 
    } 
} 
+0

歡迎您。請將此答案標記爲接受以幫助其他人發生類似問題 – Blackbelt 2013-05-14 07:22:16

+1

我已經做了:) – frustratedmonkey 2013-05-14 07:29:09

+0

嗨,我還可以問你關於這件事嗎?當然是 – frustratedmonkey 2013-05-14 08:31:38

1

在循環product是rewritten.Iniitialise每次

1

的產品變量從未在循環重新初始化。您應該爲for循環的每次迭代初始化Product。