无法使用Jackson:Java从json字符串形成POJO

时间:2014-07-18 21:40:48

标签: java json jackson

我有以下字符串:

{"networks":[["divakar","64:66:",4,1,-37,-98],["OPTUSA202B26","00:1e:2a:",3,2,-40,-98],["Mantri","00:1e:a6:1",5,6,-41,-98],["belkin.5D7","08:86:3",5,6,-38,-98]]}

我正在使用以下对象:

public static class Network{
        private ArrayList<SoftAPInfo> networks;

        public Network(ArrayList<SoftAPInfo> networks) {
            this.networks = networks;
        }

        public Network(){

        }

        public ArrayList<SoftAPInfo> getNetworks() {
            return networks;
        }

        public void setNetworks(ArrayList<SoftAPInfo> networks) {
            this.networks = networks;
        }
    }


public static class SoftAPInfo {
        private ArrayList<String> list;
        public ArrayList<String> getList() {
            return list;
        }

        public void setList(ArrayList<String> list) {
            this.list = list;
        }

        public SoftAPInfo() {

        }
}

以及以下jackson命令:

TypeReference<Network> typeRef = new TypeReference<Network>() {};
ObjectMapper mapper = new ObjectMapper();
        try {
            Object jsonObject =  mapper.readValue(response, typeRef);
            callback.onSuccess(jsonObject);
        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

但我不断得到一个例外: 无法从start_array标记中反序列化对象

有人可以帮我解决这个问题吗?

谢谢, 晴天

1 个答案:

答案 0 :(得分:-2)

问题是每个“网络”都不是JSON对象,它是一个JSON数组。如果要将普通JSON数组反序列化为Java对象,则需要提供一个自定义反序列化器,它可以理解对象顺序并可以创建适当的POJO。