简体   繁体   English

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

[英]How to convert a string to a JSON and then extract required values from the JSON object?

I have a string as below, I want to convert it to a JSON object and then extract all the values of "text" field: 我有如下字符串,我想将其转换为JSON对象,然后提取“文本”字段的所有值:

{
 "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
  }
 }
}

I've tried 我试过了

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

but it doesn't work and I don't know how to extract all the "text" values, any help would be appreciated, thank you! 但它不起作用,我也不知道如何提取所有“文本”值,将不胜感激,谢谢!

In this json values is an array, so try 在此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");
}

So at first you need to understand in JSON you need to access the value from top to bottom, not directly to the key. 因此,首先需要了解JSON,您需要从上至下访问值,而不是直接访问键。 I guess this will do what you want 我想这会做你想要的
This is for 2.3 gson library 这是针对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();
}

I hope my answer is clear enough. 我希望我的答案足够清楚。 If you have any question regarding my answer feel free to ask in the comment :) 如果您对我的回答有任何疑问,请随时在评论中提问:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM