任何人都可以更正此代码?

时间:2014-05-04 18:01:41

标签: java json

@GET
@Produces(MediaType.APPLICATION_JSON)
public List<ProductData> getAllProductList(@QueryParam("hotel_id") int hotel_id) throws SQLException{

    System.out.println("Hotel id id==="+hotel_id);

    ProductData productData=new ProductData();
    List<ProductData> products = new ArrayList<ProductData>();

    rs=stmt.executeQuery("select * from products where hotel_id="+hotel_id);

    while(rs.next()){
        productData.setProductName(rs.getString("name"));
        productData.setProductCategory(rs.getString("category"));
        productData.setProductRate(rs.getDouble("rate"));
        productData.setProductLogoPath(rs.getString("productLogoPath"));
        products.add(productData);
    }

    return products; 
}

我已经将List作为JsonObject传递。现在我试图获得像

这样的List值
void handleResponse(String response) throws JSONException {

    JSONObject jsonObject=new JSONObject(response);
        JSONArray jsonArray = jsonObject.getJSONArray("products");

}

但我无法获得List值.anyBody可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

使用方法签名类似于 - 公共响应getAllProduct .. &安培; 回来像 - 返回Response.status(Status.OK).entity(products).build();

对于intg。层使用 - 公共ClientResponse&lt;&gt; similarSignatureMethod .. &安培; 通过客户端调用,然后得到响应实体 - clientResponse.getEntity();

答案 1 :(得分:0)

有一种简单的方法可以将json字符串转换为object:

试试这个:

ObjectMapper mapper = new ObjectMapper();
POJO obj = mapper.readValue(yourJSONString, POJO.class);
相关问题