简体   繁体   中英

Convert JsonObject into String using Java

I am getting some values from mongodb via Java Spring API. For sofTagList as in image below, I am getting List<JsonObject> for that. I am trying to access the value field inside tagName . When I itereate the list, I am getting null values each and every time. I tried googling and convert that JsonObject to String. But that doesn't help me to solve my issue. I am using com.google.gson.JsonObject package. Here is the screen shot of my mongodb which I am trying to access.

mongodb,JsonObject的屏幕截图

Edit 1: Added code Snippet.

if(null!=person.getSofTagList()){
                    List<JsonObject> jsonObj = person.getSofTagList();
                    System.out.println("Size is   ========>>  "+ jsonObj.size());
                    for (JsonObject jsonObject : jsonObj) {
                        System.out.println("GetTag Name         "+jsonObject.getAsJsonObject("tagName"));                       
                    }
                }

When printing size I am getting 30 as shown in image. It loops for 30 times in for loop but all time null is the output. Hope my question is clear. Thanks in advance.

Update the code inside the for loop as below :

 JSONArray msg = (JSONArray) jsonObject.get("tagName");
            Iterator<String> iterator = msg.iterator();
            while (iterator.hasNext()) {
                System.out.println(iterator.next());
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM