如何用杰克逊解析JSON,以" ["

时间:2014-01-13 10:31:35

标签: java json google-app-engine jackson

我的字符串是这样的:

[{"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字符串?应该有一个简单的解决方案,但我找不到它。感谢

1 个答案:

答案 0 :(得分:4)

您的JSON代表TrendTags个对象的数组。您试图将其解析为好像它代表单个 TrendTags对象。

删除所有试图修改JSON的代码,然后执行:

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

那就是说,使用List通常会更好;

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