我有超过100 o的json格式的文档(Tweet)。我必须从这些文件中提取标签。我正在通过mongodb驱动程序读取这个文件。
entities=Document{
{
urls=[
],
hashtags=[
Document{
{
indices=[
89,
104
],
text=Hungry4Science
}
},
Document{
{
indices=[
105,
112
],
text=ASCO16
}
}
]}}我必须从这个结构中获取文本,然后将其插入到我的mongo集合中。每个tweet都有hashtag实体,但我不能读取较低级别的对象。
Document hash = (Document)old_status.get("entities");
new_status.append("hastags", hash.get("hashtags"));我没有得到文本,而是将整个文档作为输出:
hashtags=[
Document{
{
indices=[
73,
80
],
text=cancer
}
},
Document{
{
indices=[
81,
90
],
text=moonshot
}
},
Document{
{
indices=[
125,
133
],
text=pallonc
}
}
]我试过这样做,但没有运气。有什么帮助吗。
发布于 2016-06-03 15:51:26
Document entity = (Document)old_status.get("entities");
ArrayList<Document> hashlist =(ArrayList<Document>) entity.get("hashtags");
ArrayList<String> hashtaglist = new ArrayList<String>();
for(Document hashtag:hashlist){
String g = hashtag.getString("text");
hashtaglist.add(g);
}new_status.append("hashtags",hashtaglist); collection.insertOne(new_status);这个程序从标签中获取所有文本对象,并保存到数组中!
https://stackoverflow.com/questions/37595834
复制相似问题