0

i am getting following output

for( Iterator <Object> it = results2.iterator();it.hasNext();)
            {
                BasicDBObject result  = (BasicDBObject) it.next();
                BasicDBObject dbo = (BasicDBObject) result.get("obj");
                String title2=(String) dbo.getString("Filename");
                        system.out.println("filename"+title2);
                 JsonArray finalResult = null;
                 JsonParser  parser = new JsonParser();
                 String tags2=dbo.getString("Tags");
                 JsonElement elem   = parser.parse(tags2);

                 finalResult = elem.getAsJsonArray();
                 for(int i=0;i<finalResult .size();i++)
                 {
                    JsonElement keys=finalResult.get(i);
                    System.out.println("final Result:"+keys);

                 }
}

Output:

filename:sample.pdf
finalresult:"java"
            "pdf"
            "c++"

filename:sample1.pdf
finalresult:"java1"
            "pdf"
            "c+"

But i want to get the overall tags for all the files by removing duplicates

filename:sample.pdf
filename:sample1.pdf
finalresult:"java"
            "pdf"
            "c++"
            "java1"
            "c+"

my tag field in mondodb is

["java","php","c++]

can you please suggest me how to do this

thanks

user2545106
  • 87
  • 1
  • 6
  • 3
    you should get familiar \with Sets – Tala Jul 12 '13 at 12:36
  • Use a java Set, here is an example. http://stackoverflow.com/questions/2490178/how-to-use-java-set – okram123 Jul 12 '13 at 12:44
  • If the result is not too large, you can simply call the method toArray on the DBCursor to get an array. Then, loop trough the array the first time to print the file name and the second time to print the tags. And if you want each tag to be printed only once, you should use set as suggested by okram123. – innoSPG Jul 12 '13 at 13:07
  • possible duplicate of [How to split json object extracted from mongodb using java](http://stackoverflow.com/questions/17613994/how-to-split-json-object-extracted-from-mongodb-using-java) – Jonathan Jul 12 '13 at 14:57
  • Do you really need to use GSON to parse that value? Are the tags stored as an array in MongoDB, or are they really stored as a String? – Trisha Aug 11 '13 at 14:37

0 Answers0