Trying to return JSONObject from QueryBuilder as a List to loop through in my Component | Community
Skip to main content
Level 2
August 20, 2021
Solved

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

  • August 20, 2021
  • 4 replies
  • 1413 views

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.

 

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.

 

Edit: removed code from 3rd parties

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Kishore_Kumar_

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 @530627 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;
}

4 replies

Asutosh_Jena_
Community Advisor
Community Advisor
August 21, 2021

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!

Level 2
August 22, 2021

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!

Kishore_Kumar_
Level 9
August 21, 2021

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

}
arunpatidar
Community Advisor
Community Advisor
August 21, 2021

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;
}
Arun Patidar
Kishore_Kumar_
Kishore_Kumar_Accepted solution
Level 9
August 23, 2021

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 @530627 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;
}