Expand my Community achievements bar.

Learn about Edge Delivery Services in upcoming GEM session
SOLVED

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

Avatar

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.

 

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

Topics

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

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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 @Deleted Account 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;
}

View solution in original post

5 Replies

Avatar

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!

Avatar

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!

Avatar

Community Advisor

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

}

Avatar

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


Arun Patidar

Avatar

Correct answer by
Community Advisor

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 @Deleted Account 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;
}