如何将字符串转换为JSON,然后从JSON对象中提取所需的值?

时间:2014-10-02 02:35:28

标签: java json gson freebase

我有一个字符串如下,我想将其转换为JSON对象,然后提取“text”字段的所有值:

{
 "id": "/m/030qb3t",
  "property": {
   "/travel/travel_destination/tourist_attractions": {
    "valuetype": "object",
    "values": [
    {
     "text": "Hollywood Wax Museum",
     "lang": "en",
     "id": "/m/07_dsf",
     "creator": "/user/rumifield",
     "timestamp": "2008-12-27T17:02:32.000Z"
    },
    {
     "text": "Hollywood Walk of Fame",
     "lang": "en",
     "id": "/m/0qjfl",
     "creator": "/user/rumifield",
     "timestamp": "2008-12-27T17:02:32.000Z"
    },
    {
     "text": "TCL Chinese Theatre",
     "lang": "en",
     "id": "/m/04n2h_",
     "creator": "/user/rumifield",
     "timestamp": "2008-12-27T17:02:32.000Z"
    },
    {
     "text": "Museum of Tolerance",
     "lang": "en",
     "id": "/m/04by8k",
     "creator": "/user/rumifield",
     "timestamp": "2008-12-27T17:02:32.001Z"
    },
    {
     "text": "Getty Center",
     "lang": "en",
     "id": "/m/01mvl6",
     "creator": "/user/rumifield",
     "timestamp": "2008-12-27T17:02:32.001Z"
    },
    {
     "text": "Museum of Contemporary Art, Los Angeles",
     "lang": "en",
     "id": "/m/02816j",
     "creator": "/user/rumifield",
     "timestamp": "2008-12-27T17:02:32.001Z"
    },
    {
     "text": "Griffith Park",
     "lang": "en",
     "id": "/m/02sdph",
     "creator": "/user/rumifield",
     "timestamp": "2008-12-27T17:02:32.001Z"
    },
    {
     "text": "Hollywood Boulevard",
     "lang": "en",
     "id": "/m/035zpq",
     "creator": "/user/rumifield",
     "timestamp": "2008-12-27T17:02:32.002Z"
    },
    {
     "text": "Olvera Street",
     "lang": "en",
     "id": "/m/030svs",
     "creator": "/user/rumifield",
     "timestamp": "2008-12-27T17:02:32.002Z"
    },
    {
     "text": "Centinela Adobe",
     "lang": "en",
     "id": "/m/04drm4",
     "creator": "/user/supergmackenz",
     "timestamp": "2011-12-13T18:45:26.003Z"
    }
   ],
   "count": 27.0
  }
 }
}

我试过

JsonParser jsParser = new JsonParser();
JsonObject jsObject = (JsonObject)jsParser.parse(jsonStr);
String text=jsObject.getString("text");

但它不起作用,我不知道如何提取所有“文本”值,任何帮助将不胜感激,谢谢!

2 个答案:

答案 0 :(得分:1)

在这个json中values是一个数组,所以试试

JSONObject getObject = object.getJSONObject("property");
JSONObject block  =  getObject.getJSONObject("/travel/travel_destination/tourist_attractions");
JSONArray getArray = block.getJSONArray("values");

for(int i = 0; i < getArray.size(); i++)
{
    JSONObject jsObject= getArray.getJSONObject(i);
    String text=jsObject.getString("text");
}

答案 1 :(得分:1)

首先,您需要了解JSON,您需要从上到下访问值,而不是直接访问密钥。我想这会做你想要的 这是2.3 gson库

JsonParser jsParser = new JsonParser();
JsonObject jsObject = (JsonObject)jsParser.parse(jsonStr);
JsonObject property = jsObject.getAsJsonObject("property");
JsonObject tourist  = property.getAsJsonObject("/travel/travel_destination/tourist_attractions");
JsonArray  values   = tourist.getAsJsonArray("values"); 
for(int i=0;i<values.size();i++) {
    JsonObject data = values.get(i).getAsJsonObject();
    String text = data.get("text").getAsString();
}

我希望我的答案足够明确。如果您对我的回答有任何疑问,请随时在评论中提问:)