简体   繁体   中英

Parsing JSON using Java/Jackson

I have a small test application I'm writing in Java to parse some JSON from the Reddit API. Some sample JSON I'm looking to parse would be like this:

[
{
    "kind": "Listing",
    "data": {
        "modhash": "1jq62oyvwe15aaba7eb18b0b4363b567a00750766351e03dcc",
        "children": [
            {
                "kind": "t3",
                "data": {
                    "domain": "businessinsider.com",
                    "media_embed": {},
                    "levenshtein": null,
                    "subreddit": "Android",
                    "selftext_html": null,
                    "selftext": "",
                    "likes": null,
                    "saved": false,
                    "id": "n17u2",
                    "clicked": false,
                    "title": "CONFESSION OF A NON-APPLE-FANBOY: Even If The Samsung Galaxy Nexus Is Better, I'm Still Buying An iPhone",
                    "media": null,
                    "score": 0,
                    "over_18": false,
                    "hidden": false,
                    "thumbnail": "http://e.thumbs.redditmedia.com/sL0dCwGAvWqnY_sd.jpg",
                    "subreddit_id": "t5_2qlqh",
                    "author_flair_css_class": null,
                    "downs": 2,
                    "is_self": false,
                    "permalink": "/r/Android/comments/n17u2/confession_of_a_nonapplefanboy_even_if_the/",
                    "name": "t3_n17u2",
                    "created": 1323127132,
                    "url": "http://www.businessinsider.com/apple-iphone-versus-samsung-galaxy-nexus-2011-12",
                    "author_flair_text": null,
                    "author": "FormulaT",
                    "created_utc": 1323101932,
                    "num_comments": 0,
                    "ups": 1
                }
            }
        ],
        "after": null,
        "before": null
    }
},
{
    "kind": "Listing",
    "data": {
        "modhash": "1jq62oyvwe15aaba7eb18b0b4363b567a00750766351e03dcc",
        "children": [],
        "after": null,
        "before": null
    }
}
]

What I'm trying to accomplish is to get just a few values out of this JSON, eg the title and the author. I'm using Jackson to handle the JSON, and the code I'm using looks like this:

URLConnection conn = redditURL.openConnection();
BufferedReader buf = new BufferedReader(new InputStreamReader(conn.getInputStream()));

ObjectMapper mapper = new ObjectMapper();
RedditComment comment = mapper.readValue(buf, RedditComment.class);
Iterator itr = comment.getData().getChildren().listIterator();

I created the RedditComment and other needed classes using the JSONGen website (http://jsongen.byingtondesign.com/). However, when parsing the JSON from the BufferedReader, Jackson throws this exception:

org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of com.test.RedditAPI.RedditComment out of START_ARRAY token
at [Source: java.io.BufferedReader@3ebfbbe3; line: 1, column: 1]
at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)
at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:219)
at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:212)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromArray(BeanDeserializer.java:869)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:597)
at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2723)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1877)
at com.test.RedditAPI.main.returnRedditComment(Main.java:17)

Does anyone have any ideas? I've been scratching my head for a few hours now.

EDIT: Thanks to @Chin Boon and @Chris, I came up with the following (after switching to GSON):

Gson gson = new Gson();
List<RedditComment> comment = gson.fromJson(buf, new TypeToken<List<RedditComment>>() {}.getType());
List<RedditChildren> children = comment.get(1).getData().getChildren();
System.out.println(children.get(1).getData().getAuthor());

but that throws the following exception:

java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.test.RedditAPI.RedditChildren

Apologies if I'm being a bother, I've looked around the API and there isn't any reference of LinkedHashMaps, so I don't know why it's popping up here.

The problem is that the response is an array of entries. Try:

List<RedditComment> comment = mapper.readValue(buf,new TypeReference<List<RedditComment>>(){});

if you have not already invested too much time into Jackson, may i recommend you looking at GSON, here is a tutorial that should have you started.

Google GSON API maps your JSON string into a domain object.

http://java.sg/parsing-a-json-string-into-an-object-with-gson-easily/

Also, you can use a JSON parser to see your JSON.

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