0

我想知道如何获得 Json 值?(从下面的 JSOn 我想获得产品的“价格”,以便我可以加起来。非常感谢。

{ "StatusCode": 0
, "StatusInfo": "Processed and Logged OK"
, "PageNumber": 0
, "TotalPageCount": 1
, "TotalProductCount": 1
, "PageProductCount": 1
, "Products": [
  { "BaseProductId": "54158094"
  , "EANBarcode": "3068320055008"
  , "CheaperAlternativeProductId": ""
  , "HealthierAlternativeProductId": ""
  , "ImagePath": "http://img.tesco.com/Groceries/pi/008/3068320055008/IDShot_90x90.jpg"
  , "MaximumPurchaseQuantity": 99
  , "Name": "UNAVAILABLE: Evian Natural Mineral Water 500 Ml"
  , "OfferPromotion": ""
  , "OfferValidity": ""
  , "OfferLabelImagePath": ""
  , "Price": 0.51
  , "PriceDescription": "(Unavailable)"
  , "ProductId": "258247301"
  , "ProductType": "QuantityOnlyProduct"
  , "UnitPrice": 0.004
  , "UnitType": "100ml" } ] }
4

2 回答 2

0

您可以使用JSONObject.getDouble()方法或JSONObject.getInt()方法。

JSONObject obj = new JSONObject("{ \"StatusCode\": 0, \"StatusInfo\": \"Pr...");

JSONArray products = obj.getJSONArray("Products");
JSONObject prod = products.getJSONObject(0);

double price = prod.getDouble("Price");

总结价格,只需遍历JSONArray实例。

double sum = 0.0;
for(int i = 0; i < products.length(); i++) {
    sum += products.getJSONObject(i).getDouble("Price");
}

……

于 2012-04-06T10:41:09.183 回答
0

Price标签值不是int它的两倍,所以double如果你愿意,请使用 ,,int

然后getInt从 jsonobject 但它返回0.due0.5 值...

         try {
                    String str="{ \"StatusCode\": 0....";
            JSONObject objJsonObject = new JSONObject(str);
            JSONArray array = objJsonObject.getJSONArray("Products");

               Double d =array.getJSONObject(0).getDouble("Price");
               System.out.print(d);
            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
于 2012-04-06T10:43:42.207 回答