【问题标题】:How do I get the url of the image tag in Java?如何在 Java 中获取图像标签的 url?
【发布时间】:2016-10-26 10:37:57
【问题描述】:

ImageUrl 是:

JsonObject:

{  
   "type":"table",
   "subtype":"attribute_list",
   "doc":"https://api-v2.swissunihockey.ch/api/doc/attribute_list",
   "data":{  
      "context":null,
      "headers":[  
         {  
            "text":"Name",
            "key":"teamname",
            "long":"Name",
            "short":"Name",
            "prefer":"fit"
         },
         {  
            "text":"Logo",
            "key":"logo_url",
            "long":"Logo",
            "short":"Logo",
            "prefer":"fit"
         },
         {  
            "text":"Webseite",
            "key":"website_url",
            "long":"Webseite",
            "short":"Webseite",
            "prefer":"fit"
         },
         {  
            "text":"Teamportrait",
            "key":"portrait",
            "long":"Teamportrait",
            "short":"Teamportrait",
            "prefer":"fit"
         },
         {      
            "text":"Liga",
            "key":"liga",
            "long":"Liga",
            "short":"Liga",
            "prefer":"fit"
         },
         {  
            "text":"Anschrift",
            "key":"address",
            "long":"Anschrift",
            "short":"Anschrift",
            "prefer":"fit"
         }
      ],
      "title":"MR Krauchthal II",
      "subtitle":null,
      "tabs":[  

      ],
      "slider":null,
      "regions":[  
         {  
            "text":null,
            "rows":[  
               {  
                  "highlight":false,
                  "cells":[  
                     {  
                        "text":[  
                           "MR Krauchthal II"
                        ]
                     },
                     {  
                        "image":{  
                           "alt":"",
                           "url":"https://res.cloudinary.com/swiss-unihockey/image/upload/t_club_logo/gin0rst7ocrcuioryacs.png"
                        }
                     },
                     {  
                        "url":{  
                           "href":null,
                           "text":"Webseite"
                        }
                     },
                     {  
                        "image":{  
                           "alt":"",
                           "url":null
                        }
                     },
                     {  
                        "text":[  
                           "4. Liga"
                        ]
                     },
                     {  
                        "text":[  
                           "MR Krauchthal",
                           "Thomas"
                        ]
                     }
                  ]
               }
            ]
         }
      ]
   }
}

【问题讨论】:

    标签: java json gson geojson


    【解决方案1】:

    您需要的是 JSON 解析器。您可以使用多个库,例如 JacksonGson

    这里有一些使用 Gson 的基本 Json 处理:

    JsonObject json = new JsonParser().parse(myJsonString).getAsJsonObject();  // will parse your string to json and return it as a JsonObject
    JsonObject data = json.get("data").getAsJsonObject(); // returns the jsonObject that contains everything in "data":{...}
    JsonArray regions = data.get("regions").getAsJsonArray();  // to get an array like "regions"
    for(int i=0;i<regions.size();i++){   // loop through the array
        JsonObject region = regions.get(i).getAsJsonObject();  // get any element in the array (as JsonObject)
        String text= region.get("text").getAsString();  // get a JsonElement from your JsonObject and read it as a String (like "text")
    }
    

    它看起来不是很“干净”或“简单”,其他 Json 库会以不同的方式执行此操作,但如果您不希望在 POJO 中解析整个“数据”对象,我会推荐这种方式。您现在要做的就是使用我向您展示的方法遍历您的 json,直到您获得“url”或您想要的任何其他元素。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-14
      • 1970-01-01
      • 1970-01-01
      • 2012-05-13
      • 1970-01-01
      • 2017-06-14
      • 2017-06-18
      • 2012-10-22
      相关资源
      最近更新 更多