简体   繁体   English

在Java中解析为Json Formate时出现类型不匹配错误

[英]Type Mismatch error In Parsing to Json Formate in java

Hello EveryOne ! 大家好 ! I have a List object that i need to parse into Json type using gson1.1 jar but its is giving type mismatch error ..here is my code.. 我有一个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 Help me Thanx in advance.. Plz Guys提前帮助我Thanx ..

The return type of your method is List<Product> , which is a List of Product objects whereas you're returning jsonCartList , which is a String . 方法的return类型是List<Product> ,它是Product对象的List ,而您返回的是jsonCartList ,它是String

Hence, the TypeMismatch error. 因此, TypeMismatch错误。

To achieve what you want(to return the JSON ), change the return type of your method. 要实现您想要的(返回JSON ),请更改方法的return类型。 It should now be 现在应该是

public static String getCartList() {

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

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