2016-06-02 11 views
0

Ich habe mehr als 100o Dokument im JSON-Format (Tweets). Ich muss Hashtags aus diesen Dokumenten extrahieren. Ich lese diese Datei über mongodb-java-Treiber.Verschachtelte Json in Mongodb Java

entities=Document{ 
    { 
    urls=[ 

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

Ich muss Text von dieser Struktur dann werde ich in meine Mongo-Sammlung einfügen. Jeder Tweet hat eine Hashtag-Entity, aber ich kann die Objekte der unteren Ebene nicht lesen.

 Document hash = (Document)old_status.get("entities"); 
     new_status.append("hastags", hash.get("hashtags")); 

Statt Text zu bekommen, habe ich ganzes Dokument als meine Ausgabe:

hashtags=[ 
    Document{ 
    { 
     indices=[ 
     73, 
     80 
     ], 
     text=cancer 
    } 
    }, 
    Document{ 
    { 
     indices=[ 
     81, 
     90 
     ], 
     text=moonshot 
    } 
    }, 
    Document{ 
    { 
     indices=[ 
     125, 
     133 
     ], 
     text=pallonc 
    } 
    } 
] 

Ich habe versucht, wie dies aber kein Glück. Irgendwelche Hilfe bitte.

+0

Ich habe die Antwort für dieses Problem erhalten !!! Vielleicht kann diese Antwort jedem helfen. – prabhu

Antwort

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); 

Dieses Programm wird alle Textobjekt von hashtag und in Arraylist speichern !!!