2

I have a String with a JSON that contains a list. Its something like this:

{
    "counts":[
        {"foo827138": 123124, "bar2918":312312, "something_else321-313":2321321},
        {"foo1231412": 4321, "bar1231515":123, "something_else3012931":7282},
        {"foo1210820": 1234, "bar10293810":3112, "something_else12094":1321}
    ]
}

After parsing it with BasicDBObject.parse():

DBObject doc = BasicDBObject.parse(jsonString);  

How can I get the keys and values on counts list?

PS.: doc.get("counts") returns a BasicDBList object. I can only iterate over this with:

for (Object a : doc) {
    // Something
}

Object does not provide a get() method or something like. So I don't know how to get any of my key names and respective values.

2
  • when you reference BasicDBObject are you talking about the MongoDB instance of it? can you provide the code where you are calling parse()? Commented Apr 20, 2018 at 20:26
  • Yes, it is a MongoDB instance of it. I've edited the question providing the exact scenario. Commented Apr 20, 2018 at 20:50

2 Answers 2

1
Document document = Document.parse(jsonString);
List<Document> countsArray = document.get("counts",List.class);
for (Document doc : countsArray){
    doc.keySet(); //keys
    doc.values(); //values
    for (Map.Entry entry : doc.entrySet()){
        // iterate over each pair
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Have you tried yourObject.get("keyname")

1 Comment

The keys are randomic

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.