我想从json中提取数组值

时间:2014-07-25 13:47:17

标签: java json

{
  "data": {
    "tech-spec": "377.pdf",
    "additional-details": {
      "item": [
    "Currency:GBP",
    "Scaleprice:",
    "Priceunit:"
      ]
    },
    "currency-code": "GBP"
  }
}

上面是我的json字符串,我想提取Currency,Scaleprice,Priceunit的值,它是使用java的“item”数组的值

1 个答案:

答案 0 :(得分:0)

看起来它是无效的JSON,因为数据似乎不在{或[。

您可以使用

操纵字符串
JSONObject jsonData= new JSONObject(data);
if (jsonData.has("data")) { 
   JSONObject json = new JSONObject(data.getString("data"));
   String tech-spec = json.getString("tech-spec");
   JSONObject additionalDetails = json.getJSONObject("additional-details");

   if (additionalDetails.has("item")) { 
   JONArray item = new JSONArray(additionalDetails.getString("item"));
   for (int i = 0; i <=  item.length(); i++) {
    JSONObject itemObj = new JSONObject(item.getJsonObject(i));
    String Currency = itemObj.getString("Currency");    
    // and the others,...
    }
   }
   String currencyCode = json.getString("currency-code");
 }