简体   繁体   English

将JSON对象传递到HTML页面

[英]Pass JSON object to HTML page

I use Rest API, and I need to pass a JSON object and use it in an HTML/JSP page. 我使用Rest API,并且需要传递JSON对象并在HTML / JSP页面中使用它。

public Response getOffers(@FormParam("myParam") String inParam){
    JSONObject json = new JSONObject();
    ..
    return Response.status(200).entity(json).build();
} 

I need to redirect to another html page, and include the json object as a param ( in order to use it in my js ) 我需要重定向到另一个HTML页面,并将json对象作为参数包含(以便在我的js中使用它)

Any help is appreciated 任何帮助表示赞赏

Usually you define the structure of the object you would like to return as a class, such as: 通常,您定义要作为类返回的对象的结构,例如:

public class Offer
{
  private int id;

  public int getID() { return this.id; }
  public void setID(int id) { this.id = id; }
}

Then you can modify the signature of your method to return a list of offer objects: 然后,您可以修改方法的签名以返回商品对象列表:

@GET
@Path("/offers")
@Produces("application/json")
public List<Offer> getOffers(/*...*/)
{
  List<Offer> result = new ArrayList<>();

  Offer myOffer = new Offer();
  myOffer.setID(10);

  result.add(myOffer);

  return result;
}

Hope I got your question right ;) 希望我能正确回答你的问题;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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