类型不匹配错误在Java中解析为Json格式

时间:2013-03-11 07:49:22

标签: java json list parsing

Hello EveryOne!                    我有一个List对象,我需要使用gson1.1 jar解析为Json类型,但它给出了类型不匹配错误..这是我的代码..

public static List<Product> getCartList() {
    List<Product> cartList = new Vector<Product>(cartMap.keySet().size());
    for(Product p : cartMap.keySet()) {
        cartList.add(p);
    }

     Gson gson = new Gson();
     // convert your list to json
     String jsonCartList = gson.toJson(cartList);
     // print your generated json
     System.out.println("jsonCartList: " + jsonCartList);

     return jsonCartList;

        }

Plz Guys提前帮助我Thanx ..

1 个答案:

答案 0 :(得分:1)

您的方法的return类型为List<Product>,这是ListProduct个对象,而您正在返回jsonCartList,这是{ {1}}。

因此, TypeMismatch 错误。

要实现您想要的效果(返回String),请更改方法的JSON类型。现在应该是

return
相关问题