Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

Trying to return JSONObject from QueryBuilder as a List to loop through in my Component

SocialTaylor
Level 3
Level 3

Hello! I have created a Servlet that accesses the QueryBuilder API and outputs that data on a custom backend point, as you can see below.

 

Screen Shot 2021-08-19 at 9.52.03 PM.png

 

The next step was to pull this JSON in from the Service we created into our Component, which I have set up as a JSONObject, though you are unable to loop through those in the htl. I have been trying to covert those from a JSONObject to a List so that we can loop through the results that we get from that QueryBuilder, but I haven't been able to get that converted correctly. Is there something that I am doing incorrectly on this? I'm not super well versed in Java, so I'm not sure if this is a silly mistake in my code or not.

 

Screen Shot 2021-08-19 at 9.52.38 PM.png

Screen Shot 2021-08-19 at 9.52.52 PM.png

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

6 Replies
Asutosh_Jena_
Community Advisor
Community Advisor

Hi @SocialTaylor 

 

If I understood your questions correctly you want to loop through the "results" array in the JSON and print those value in a list in HTL i.e., 

 

path: some value

title: some title

 

path: some value

title: some tile

 

path: some value

title: some title

 

Let me know if my understanding is correct and I can help in getting the list.

 

Thanks!

SocialTaylor
Level 3
Level 3

Hello @Asutosh_Jena_ !

 

That would be correct. We'll be needing to create a component in which we can loop through Pages as Blog Posts so that we can create sort of a list of cards for users to have a preview and link out to the page itself!

 

Thanks for the reply again haha. You have been a huge help on this project!

kishorekumar14
Level 9
Level 9

By looking at your code, Inner POJO class is not having Getters/Setters or public variables. So it won't be eligible for Serialization/Deserialization. and also your inner POJO class needs to be static for jackson serialization.

 

So if you are using lombok dependency in your project, it has to be like below.

 

public static @Data class Page {
 String path;
 String title;
}

else

 

public static class Page {

String path;
String title;
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}

}
Arun_Patidar
Community Advisor
Community Advisor

Hi,

you could you please ckeck your Page class where the identifiers are defined in Capital case but json contains in small case.

public class Page{
String title;
String path;
}
kishorekumar14
Level 9
Level 9

Hi @SocialTaylor ,

 

By looking at your code, Inner POJO class is not having Getters/Setters or public variables. So it won't be eligible for Serialization/Deserialization. and also your inner pojo class needs to be static for jackson serialization

 

So if you are using lombok dependency in your project, it has to be like below.

 

public static @data class Page {
 String path;
 String title;
}

 

else

 

public static class Page {

String path;
String title;
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}

SocialTaylor
Level 3
Level 3

Hey @kishorekumar14@Asutosh_Jena_, and @Arun_Patidar!

 

Thanks for taking a look at this and giving some advice. I went ahead and made those updates and tested it, but it seemed like I was still having an issue in the Jackson ObjectMapper to move it from a JSONObject (or JSONArray) to the List. I kept getting this error: "The method readValue(JsonParser, Class<T>) in the type ObjectMapper is not applicable for the arguments (JSONArray, new TypeReference<List<SearchQueryImpl.Page>>(){})".

 

To fix this, for anyone else encountering the same problems as I, I just took the JSONArray that I was retrieving from my results, turning it into a String (look at the documentation here: https://www.baeldung.com/jackson-object-mapper-tutorial) and then feeding that into the ObjectMapper, which is creating the list after adding exceptions. See screenshot below for finished code!

 

Screen Shot 2021-08-23 at 10.34.30 AM.pngScreen Shot 2021-08-23 at 10.34.18 AM.pngScreen Shot 2021-08-23 at 10.34.11 AM.png