如何使用Jackson像这样反序列化阵列?

时间:2015-05-15 05:38:25

标签: java android json jackson

如何简单地反序列化?我的JSON看起来像:

{
"ok": 1,
"data": {
    "A": [
        {
            "brand": "Aman",
            "logo": "http://zxmall.bluedoor.com.cn/uploads/logo/61.png"
        }
    ],
    "B": [
        {
            "brand": "Base-DS",
            "logo": "http://zxmall.bluedoor.com.cn/uploads/logo/24.png"
        },
        {
            "brand": "BB",
            "logo": "http://zxmall.bluedoor.com.cn/uploads/logo/46.png"
        },
        {
            "brand": "Banane",
            "logo": "http://zxmall.bluedoor.com.cn/uploads/logo/11.png"
        }

    ],
    "C": [
        {
            "brand": "cat",
            "logo": "http://zxmall.bluedoor.com.cn/uploads/logo/78.png"
        },
        {
            "brand": "car",
            "logo": "http://zxmall.bluedoor.com.cn/uploads/logo/129.png"
        }
    ]
}
}

来自[A-Z]。

我的代码如下:

ReslutMod data = JsonUtil.jsonToMod(json, ReslutMod.class);

在JsonUtil中:

public static  <T> T jsonToMod(String jsonString, Class<T> clazz) {
    if (StringUtils.isEmpty(jsonString)) {
         return null;
    }
    try {
        return JsonMapper
                .getInstance()
                .getJsonMapper().readValue(jsonString, clazz);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

的JavaBean:

 public class ReslutMod {
    public int ok;
    public BrandListMod data;
 }

JavaBean2:

 public class BrandListMod {
    public ArrayList<CardBrandModel> A;
    public ArrayList<CardBrandModel> B;
    public ArrayList<CardBrandModel> C;
    public ArrayList<CardBrandModel> D;
    public ArrayList<CardBrandModel> E;
    public ArrayList<CardBrandModel> F;
    public ArrayList<CardBrandModel> G;
    public ArrayList<CardBrandModel> H;
    public ArrayList<CardBrandModel> I;
    public ArrayList<CardBrandModel> J;
    public ArrayList<CardBrandModel> K;
    public ArrayList<CardBrandModel> L;
    public ArrayList<CardBrandModel> M;
    public ArrayList<CardBrandModel> N;
    public ArrayList<CardBrandModel> O;
    public ArrayList<CardBrandModel> P;
    public ArrayList<CardBrandModel> Q;
    public ArrayList<CardBrandModel> R;
    public ArrayList<CardBrandModel> S;
    public ArrayList<CardBrandModel> T;
    public ArrayList<CardBrandModel> U;
    public ArrayList<CardBrandModel> V;
    public ArrayList<CardBrandModel> W;
    public ArrayList<CardBrandModel> X;
    public ArrayList<CardBrandModel> Y;
    public ArrayList<CardBrandModel> Z;
}

我认为这不是一个好方法。我该怎么办?

1 个答案:

答案 0 :(得分:1)

您可以在此处使用地图。

电子。 G。 :

function modal(id, url) {
 $("#verficationuser").modal('show');

   $("#passwordverify").submit(function(e){
     e.preventDefault();
     alert(id, url);
   });

}

类CardBrandModel将是这样的:

class ReslutMod {
    public int ok;
    public Map<String, ArrayList<CardBrandModel>> data;

    public int getOk() {
        return ok;
    }

    public void setOk(int ok) {
        this.ok = ok;
    }

    public Map<String, ArrayList<CardBrandModel>> getData() {
        return data;
    }

    public void setData(Map<String, ArrayList<CardBrandModel>> data) {
        this.data = data;
    }

}