我最终希望使用JSON数据集创建反向索引。我知道如何解析一个JSON对象,但是如何遍历多个JSON对象呢?这是我的工作:
文件1:
@Configuration
class TestConfig
@Autowired constructor(private val props: MyServiceProps) {
@Bean
fun getMyService(): MyService {
return props.getService()
}
}
我的代码:
{
"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"
}
现在,我要遍历此JSON文件,该JSON文件在数组中具有2个JSON对象:
文件2:
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;
}
}
在doc_id是唯一键的情况下,如何使用Jackson调整上面的代码?我希望能够为每个doc_id返回每个JSON对象中的所有数据。
答案 0 :(得分:3)
要使用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 :(得分:2)
您可以尝试将其放在列表中,以便对其进行迭代:
List<Tweet> data = mapper.readValue(json, new TypeReference<List<Tweet>>(){});
答案 2 :(得分:2)
我建议使用TypeFactory创建一个CollectionType
并使用它来将JSON解析为List<Tweet>
。
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();
}
}
}