2017-01-16 7 views
0

だから私はこだわっています。私はJSonで経験しており、JSONファイルから異なる注文のtotal_priceをすべて取得しようとしています。私はファイルを読むことができますが、私はその特定の情報を取得する問題を持っているすべての合計。JSonファイルから1つの特定のデータを抽出して、すべてを集計します

これまでのコードは次のとおりです。 JSONファイルの場合

public static void main(String[] args) { 




     JSONParser parser = new JSONParser(); 

     try { 

      Object obj = parser.parse(new FileReader(
        "/orders.json")); 

      JSONObject jsonObject = (JSONObject) obj; 


      JSONArray orderList = (JSONArray) jsonObject.get("orders"); 

      System.out.println("\norders"); 
      Iterator<String> iterator = orderList.iterator(); 
      while (iterator.hasNext()) { 

       System.out.println(iterator.next()); 
      } 
      obj = parser.parse(sCurrentLine); 
    JSONArray jsonArray = (JSONArray) obj; 
    for(obj : jsonArray){ 
     JSONObject jsonObject = (JSONObject)obj; 
     JSONObject realTitle = (JSONObject)jsonObject.get("0"); 
     String name = (String) realTitle.get("title"); 
    } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } 

{ 
"orders":[ 
{ 
"id":4251070723, 
"email":"[email protected]", 
"closed_at":null, 
"created_at":"2016-12-05T23:16:40-05:00", 
"updated_at":"2016-12-05T23:16:40-05:00", 
"number":137, 
"note":null, 
"token":"c460f260f4f38b8a2b1f78e6efd0140e", 
"gateway":"", 
"test":false, 
"total_price":"179.04", 
"default":true 
}, 
{ 
"id":4251070787, 
"email":"[email protected]", 
"closed_at":null, 
"created_at":"2016-12-05T23:16:40-05:00", 
"updated_at":"2016-12-05T23:16:41-05:00", 
"number":138, 
"note":null, 
"token":"54c4f1735cfec8f98ad16ae5e9a161cd", 
"gateway":"", 
"test":false, 
"total_price":"14.62" 
} 
] 
} 

私が手にエラーが(OBJ:jsonArray)のためである私に問題を引き起こしている、正直なところ、私は右にしてもだとは思いませんパス。

ありがとうございました!

+1

すべての注文の合計価格。 "私に問題を引き起こしている"とは何も言わない。そしてコンパイルするコードを投稿してください。 'sCurrentLine'とは何ですか? 1つのファイルを解析する必要がある場合、なぜparse()を何度か呼び出すのですか? –

答えて

0

あなたのコードには明らかなコンパイル時エラーがあります。 forループでは、やはりget(0)をやろうとしていますが、これは間違っています。

私はコードを改良しました。

これを試してください。その適切な結果は以下の通りです

各注文の価格データ - {4251070723 = 179.04,4251070787 = 14.62}です。

エラーが発生した場合は、その後、完全かつ正確なERORを投稿--193.66

public static void main(String[] args) { 
     JSONParser parser = new JSONParser(); 
     Map<String, String> priceData = new HashMap<>(); 
     BigDecimal totalPrice = BigDecimal.ZERO; 
     String orderId = null; 
     String orderPrice = null; 

     try { 
      String sCurrentLine = null; 

      Object obj = parser.parse(new FileReader(
        "orders.json")); 

      JSONObject jsonObject = (JSONObject) obj; 
      JSONArray orderList = (JSONArray)(jsonObject.get("orders")); 
      System.out.println("Complete order list --"+orderList.toString()); 

      for(Object singleOrder : orderList) { 
       JSONObject singleArrayData = (JSONObject) singleOrder; 
       orderId = singleArrayData.get("id").toString(); 
       orderPrice = singleArrayData.get("total_price").toString(); 
       priceData.put(orderId, orderPrice); 
       totalPrice = totalPrice.add(new BigDecimal(orderPrice)); 
      } 
      System.out.println("The price data of each order --"+priceData.toString()); 
      System.out.println("The total Price of all order --"+totalPrice); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } 
+0

それは素晴らしいです!あなたの助けをありがとう! – Suligane

関連する問題