如何使用Jackson迭代JSON对象?

3
我希望能够使用我的JSON数据集创建倒排索引。我知道如何解析单个JSON对象,但是如何迭代许多对象?以下是我的工作代码:
文件1:
{
    "doc_id": "2324jos",
    "screen_name": "twitter_user101",
    "tweet_text": "Its a beautiful day to be productive",
    "hashtags": "[]",
    "links": "[]",
    "place_type": "city",
    "place_name": "Evergreen Park",
    "created_at": "2019-02-08 22:24:03"
}

我的代码:

public class ParseJson {
    public static void main(String[] args) throws Exception {
        // this is the key object to convert JSON to Java
        Tweet tweet;
        ObjectMapper mapper = new ObjectMapper();
        try {
            File json = new File("test.json");
            tweet = mapper.readValue(json, Tweet.class);
            System.out.println("Java object created from JSON String :");
            System.out.println(tweet);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

public class Tweet {
    public String doc_id;
    public String screen_name;
    public String tweet_text;
    public String hashtags;
    public String links;
    public String place_type;
    public String place_name;
    public String created_at;

    public Tweet() {

    }

    public Tweet(String doc_id, String screen_name, String tweet_text, String hashtags, String links, String place_type, String place_name, String created_at) {
        this.doc_id = doc_id;
        this.screen_name = screen_name;
        this.tweet_text = tweet_text;
        this.hashtags = hashtags;
        this.links = links;
        this.place_name = place_name;
        this.place_type = place_type;
        this.created_at = created_at;
    }

    @Override
    public String toString() {
        return doc_id + screen_name + tweet_text;
    }
}

现在,我希望遍历这个JSON文件,其中包含一个数组中的2个JSON对象:
文件2:
[
    {
        "doc_id": "2324jos",
        "screen_name": "b'LIBBYRULZ'",
        "tweet_text": "@ABC ya'll be lying",
        "hashtags": "[]",
        "links": "[]",
        "place_type": "city",
        "place_name": "Evergreen Park",
        "created_at": "2019-02-08 22:24:03"
    },
    {
        "doc_id": "8982hol",
        "screen_name": "b'eddylee_1'",
        "tweet_text": "Hungry for money",
        "hashtags": "[]",
        "links": "[]",
        "place_type": "city",
        "place_name": "Manhattan",
        "created_at": "2/7/2019 17:01"
    }
]     

我该如何使用Jackson调整上述代码,使doc_id成为唯一键?我希望能够返回每个doc_id的JSON对象中的所有数据。
3个回答

4

使用Jackson解析JSON对象数组的方法如下:

Tweet[] tweets = mapper.readValue(json, Tweet[].class);

应该可以解决问题。请见下文:
public class ParseJson {
    public static void main(String[] args) throws Exception {

        Tweet[] tweets;
        ObjectMapper mapper = new ObjectMapper();
        try {
            File json = new File("test.json");
            tweets = mapper.readValue(json, Tweet[].class);
            System.out.println("Java object created from JSON String :");
            Arrays.asList(tweets).forEach(System.out::println); // Prints each element in the array
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

1
谢谢 - 你能否提供更多上下文信息展示一下?我遇到了错误:org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of Tweet out of START_ARRAY token。 - Hana
你遇到了这个异常是因为你试图将一个JSON数组映射到Java对象。你需要将JSON数组映射到Java对象列表或Java对象数组中的一个。 - Matt
谢谢Matt - 我还是有些问题,你能再使用你编辑过的代码吗?如果可以的话,我会把这个标记为已接受的答案 :) - Hana
刚刚将您的ParseJson类添加到上面的答案中了! - Matt

3
你可以尝试将它放在一个列表中,这样你就可以对它进行迭代:
List<Tweet> data = mapper.readValue(json, new TypeReference<List<Tweet>>(){});

2
我建议使用TypeFactory创建一个CollectionType,并将其用于解析JSON为List。"Original Answer"的翻译是"最初的回答"。
CollectionType tweetListType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, Tweet.class);
List<Tweet> tweets = mapper.readValue(json, tweetListType);
tweets.forEach(System.out::println);

这是您分享的完整示例:

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ParseJson {
    public static void main(String[] args) {
        // this is the key object to convert JSON to Java
        ObjectMapper mapper = new ObjectMapper();
        try {
            File json = new File("test.json");
            CollectionType tweetListType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, Tweet.class);
            List<Tweet> tweets = mapper.readValue(json, tweetListType);
            System.out.println("Java objects created from JSON String:");
            tweets.forEach(System.out::println);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

这是什么导入? - Hana
导入CollectionType类型? import com.fasterxml.jackson.databind.type.CollectionType; - Samuel Philipp
太棒了,谢谢!这个解决方案很有效,让我更好地理解了集合类型! - Hana

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接