如何在Java中获取图像标记的URL?

时间:2016-10-26 10:37:57

标签: java json gson geojson

ImageUrl是:

https://res.cloudinary.com/swiss-unihockey/image/upload/t_club_logo/gin0rst7ocrcuioryacs.png

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

1 个答案:

答案 0 :(得分:0)

您需要的是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")
}

它看起来不太干净&#34;干净&#34;或者&#34;容易&#34;和其他Json图书馆会以不同的方式做到这一点,但如果你不想要整个&#34;数据&#34;我会推荐这种方式。要在POJO中解析的对象。你现在所要​​做的就是,用你向我展示的方法走过你的json,直到你得到&#34; url&#34;或者你想要的任何其他元素。