简体   繁体   中英

How to parse JSON with Jackson that starts with “[”

My string is like this:

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

I can parse this json string when I remove "[" and "]" from first and last character by following code:

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;
    }

And my TrendsTag class is this:

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;
    }
}

}

Since I have one object in array so it's possible to remove "[" and "]" from first and last chatacter. But this is not the solution. My question is how to parse the json string with "[" and "]" characters? There should be a simple solution but I cannot find it. Thanks

Your JSON represents an array of your TrendTags objects. You're attempting to parse it as if it represented a single TrendTags object.

Get rid of all that code trying to modify the JSON, and just do:

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

That said, using a List is generally better;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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