【问题标题】:Nested Json in Mongodb javaMongodb java中的嵌套Json
【发布时间】:2016-06-02 15:16:17
【问题描述】:

我有 100 多个 json 格式的文档(推文)。我必须从这些文档中提取主题标签。我正在通过 mongodb-java 驱动程序读取这个文件。

entities=Document{
  {
    urls=[

    ],
    hashtags=[
      Document{
        {
          indices=[
            89,
            104
          ],
          text=Hungry4Science
        }
      },
      Document{
        {
          indices=[
            105,
            112
          ],
          text=ASCO16
        }
      }
    ]}}

我必须从这个结构中获取文本,然后我将插入到我的 mongo 集合中。每条推文都有标签实体,但我无法读取较低级别的对象。

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

我试过这样,但没有运气。 请帮忙。

【问题讨论】:

  • 我得到了这个问题的答案!!!可能这个答案可以帮助任何人。

标签: json twitter4j mongodb-java


【解决方案1】:
        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);

这个程序从标签中获取所有文本对象并保存到数组列表中!!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-09
    • 1970-01-01
    • 2015-02-20
    • 2015-02-07
    • 2023-02-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多