2014-01-13 78 views
-1

我的字符串是這樣的:解析JSON字符串與傑克遜庫

[{"trends":[{"name":"#Happy16thPoniGoyangLimitedEditionJKT48","url":"http:\/\/twitter.com\/search?q=%23Happy16thPoniGoyangLimitedEditionJKT48","promoted_content":null,"query":"%23Happy16thPoniGoyangLimitedEditionJKT48","events":null},{"name":"#SemihVAROLTAYFAileHaftaSonuTakibi","url":"http:\/\/twitter.com\/search?q=%23SemihVAROLTAYFAileHaftaSonuTakibi","promoted_content":null,"query":"%23SemihVAROLTAYFAileHaftaSonuTakibi","events":null},{"name":"#JeeveTeriJodi","url":"http:\/\/twitter.com\/search?q=%23JeeveTeriJodi","promoted_content":null,"query":"%23JeeveTeriJodi","events":null},{"name":"#Tolga\u00D6\u011F\u00FCt\u0130leTakiple\u015Fme","url":"http:\/\/twitter.com\/search?q=%23Tolga%C3%96%C4%9F%C3%BCt%C4%B0leTakiple%C5%9Fme","promoted_content":null,"query":"%23Tolga%C3%96%C4%9F%C3%BCt%C4%B0leTakiple%C5%9Fme","events":null},{"name":"#CNEnjoyMondayyy","url":"http:\/\/twitter.com\/search?q=%23CNEnjoyMondayyy","promoted_content":null,"query":"%23CNEnjoyMondayyy","events":null},{"name":"Medha Patkar","url":"http:\/\/twitter.com\/search?q=%22Medha+Patkar%22","promoted_content":null,"query":"%22Medha+Patkar%22","events":null},{"name":"Asaram Bapuji","url":"http:\/\/twitter.com\/search?q=%22Asaram+Bapuji%22","promoted_content":null,"query":"%22Asaram+Bapuji%22","events":null},{"name":"Tune Talk","url":"http:\/\/twitter.com\/search?q=%22Tune+Talk%22","promoted_content":null,"query":"%22Tune+Talk%22","events":null},{"name":"Golden Globes 2014","url":"http:\/\/twitter.com\/search?q=%22Golden+Globes+2014%22","promoted_content":null,"query":"%22Golden+Globes+2014%22","events":null},{"name":"Game of Thrones Season 4","url":"http:\/\/twitter.com\/search?q=%22Game+of+Thrones+Season+4%22","promoted_content":null,"query":"%22Game+of+Thrones+Season+4%22","events":null}],"as_of":"2014-01-13T09:59:22Z","created_at":"2014-01-13T09:07:24Z","locations":[{"name":"Worldwide","woeid":1}]}] 

我可以分析,當我用下面的代碼從第一個和最後一個字符去掉「[」和「]」這個JSON字符串:

private TrendTags getTrendTagsJSON(String jsonString) { 
     TrendTags trendTags = null; 
     jsonString = jsonString.substring(1, jsonString.length()-1); 

     try { 
      //create ObjectMapper instance 
      ObjectMapper objectMapper = new ObjectMapper(); 
      objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); 

      //convert json string to object 
      trendTags = objectMapper.readValue(jsonString, TrendTags.class); 
      System.out.println(trendTags); 

     } catch (JsonParseException e) { 
      System.out.println(e.getMessage()); 
     } catch (JsonMappingException e) { 
      System.out.println(e.getMessage()); 
     } catch (IOException e) { 
      System.out.println(e.getMessage()); 
     } 

     return trendTags; 
    } 

而且我TrendsTag類是這樣的:

public class TrendTags { 

@JsonProperty("trends") 
private Trend[] trend; 
@JsonProperty("locations") 
private TrendLocation[] trendLocation; 

@Override 
public String toString() { 
    return "TrendTags{" + 
      "trend=" + Arrays.toString(trend) + 
      ", trendLocation=" + Arrays.toString(trendLocation) + 
      '}'; 
} 

public Trend[] getTrend() { 
    return trend; 
} 

public void setTrend(Trend[] trend) { 
    this.trend = trend; 
} 

public TrendLocation[] getTrendLocation() { 
    return trendLocation; 
} 

public void setTrendLocation(TrendLocation[] trendLocation) { 
    this.trendLocation = trendLocation; 
} 

/************************ 
* Trend item class  * 
************************/ 
public static class Trend { 
    private String name; 
    private String url; 
    private String query; 

    @Override 
    public String toString() { 
     return "Trend {" + 
       "name='" + name + '\'' + 
       ", url='" + url + '\'' + 
       ", query='" + query + '\'' + 
       '}'; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getUrl() { 
     return url; 
    } 

    public void setUrl(String url) { 
     this.url = url; 
    } 

    public String getQuery() { 
     return query; 
    } 

    public void setQuery(String query) { 
     this.query = query; 
    } 
} 

/************************ 
* Trend location class * 
************************/ 
public static class TrendLocation { 

    private String name; 
    private int woeid; 

    @Override 
    public String toString() { 
     return "TrendLocation{" + 
       "name='" + name + '\'' + 
       ", woeid=" + woeid + 
       '}'; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public int getWoeid() { 
     return woeid; 
    } 

    public void setWoeid(int woeid) { 
     this.woeid = woeid; 
    } 
} 

}

由於我在數組中有一個對象,因此可以從第一個和最後一個chatacter中刪除「[」和「]」。但這不是解決方案。 我的問題是如何解析帶有「[」和「]」字符的json字符串?應該有一個簡單的解決方案,但我找不到它。謝謝

+0

你的JSON是你的'TrendTags'類的**數組**。 –

+0

是的,我知道。我的問題是如何在代碼中定義它,並在每個對象沒有標籤時捕獲每個「TrendTags」對象。 – Hesam

回答

3

您的JSON代表您的TrendTags對象的陣列。你試圖解析它,就好像它代表了一個單個的TrendTags對象。

擺脫所有的代碼試圖修改JSON,只是做:

TrendTags[] trendTags = 
    objectMapper.readValue(jsonString, TrendTags[].class); 

也就是說,使用List一般是越多越好;

List<TrendTags> trendTags = 
    objectMapper.readValue(jsonString, new TypeReference<List<TrendTags>>(){}); 
+0

確切地說,他們都像魅力:)感謝的人。 – Hesam