Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Jackson解析JSON对象的JSON数组引发JSON映射异常,如何修复?_Java_Android_Json_Jackson - Fatal编程技术网

Java 使用Jackson解析JSON对象的JSON数组引发JSON映射异常,如何修复?

Java 使用Jackson解析JSON对象的JSON数组引发JSON映射异常,如何修复?,java,android,json,jackson,Java,Android,Json,Jackson,这是我的JSON: [ { "_id": "574f1840d21303358a03d78b", "name": "Bank One of Kerala", "period_from": "2016-03-12T00:00:00.000Z", "period_to": "2016-03-12T00:00:00.000Z", "status": true,

这是我的JSON:

    [
        {
           "_id": "574f1840d21303358a03d78b",
           "name": "Bank One of Kerala",
           "period_from": "2016-03-12T00:00:00.000Z",
           "period_to": "2016-03-12T00:00:00.000Z",
           "status": true,
           "ins": "Get an account at Bank One of Kerala :P",
           "fields": [
               {
                   "field": "Account Number",
                   "key": "acc"
               },
               {
                   "field": "IFSC",
                   "key": "ifsc"
               },
               {
                   "field": "Branch",
                   "key": "branch"
               },
               {
                   "field": "Name",
                   "key": "name"
               },
               {
                   "field": "PAN",
                   "key": "pan"
               }
          ]
    },
    {
        "_id": "574f18e4d21303358a03d78c",
        "name": "Bank Two of India",
        "period_from": "2016-03-12T00:00:00.000Z",
        "period_to": "2016-03-12T00:00:00.000Z",
        "status": true,
        "ins": "Get an account at Bank Two of India",
        "fields": [
            {
                "field": "Account Number",
                "key": "acc"
            },
            {
                "field": "IFSC",
                "key": "ifsc"
            },
            {
                "field": "Branch",
                "key": "branch"
            },
            {
                "field": "Name",
                "key": "name"
            }
    ]
  }
]
这是我的模型课:

ShreyPojo.class

    @JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
        "_id",
        "name",
        "period_from",
        "period_to",
        "status",
        "ins",
        "fields"
})
public class ShreyPojo
{
    @JsonProperty("_id")
    private String id;
    @JsonProperty("name")
    private String name;
    @JsonProperty("period_from")
    private String periodFrom;
    @JsonProperty("period_to")
    private String periodTo;
    @JsonProperty("status")
    private Boolean status;
    @JsonProperty("ins")
    private String ins;
    @JsonProperty("fields")
    private List<Field> fields = new ArrayList<Field>();
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    /**
     *
     * @return
     * The id
     */
    @JsonProperty("_id")
    public String getId() {
        return id;
    }

    /**
     *
     * @param id
     * The _id
     */
    @JsonProperty("_id")
    public void setId(String id) {
        this.id = id;
    }

    /**
     *
     * @return
     * The name
     */
    @JsonProperty("name")
    public String getName() {
        return name;
    }

    /**
     *
     * @param name
     * The name
     */
    @JsonProperty("name")
    public void setName(String name) {
        this.name = name;
    }

    /**
     *
     * @return
     * The periodFrom
     */
    @JsonProperty("period_from")
    public String getPeriodFrom() {
        return periodFrom;
    }

    /**
     *
     * @param periodFrom
     * The period_from
     */
    @JsonProperty("period_from")
    public void setPeriodFrom(String periodFrom) {
        this.periodFrom = periodFrom;
    }

    /**
     *
     * @return
     * The periodTo
     */
    @JsonProperty("period_to")
    public String getPeriodTo() {
        return periodTo;
    }

    /**
     *
     * @param periodTo
     * The period_to
     */
    @JsonProperty("period_to")
    public void setPeriodTo(String periodTo) {
        this.periodTo = periodTo;
    }

    /**
     *
     * @return
     * The status
     */
    @JsonProperty("status")
    public Boolean getStatus() {
        return status;
    }

    /**
     *
     * @param status
     * The status
     */
    @JsonProperty("status")
    public void setStatus(Boolean status) {
        this.status = status;
    }

    /**
     *
     * @return
     * The ins
     */
    @JsonProperty("ins")
    public String getIns() {
        return ins;
    }

    /**
     *
     * @param ins
     * The ins
     */
    @JsonProperty("ins")
    public void setIns(String ins) {
        this.ins = ins;
    }

    /**
     *
     * @return
     * The fields
     */
    @JsonProperty("fields")
    public List<Field> getFields() {
        return fields;
    }

    /**
     *
     * @param fields
     * The fields
     */
    @JsonProperty("fields")
    public void setFields(List<Field> fields) {
        this.fields = fields;
    }

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    @JsonAnySetter
    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }

}
 @JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
        "field",
        "key"
})

    public class Field
    {

        @JsonProperty("field")
        private String field;
        @JsonProperty("key")
        private String key;
        @JsonIgnore
        private Map<String, Object> additionalProperties = new HashMap<String, Object>();

        /**
         *
         * @return
         * The field
         */
        @JsonProperty("field")
        public String getField() {
            return field;
        }

        /**
         *
         * @param field
         * The field
         */
        @JsonProperty("field")
        public void setField(String field) {
            this.field = field;
        }

        /**
         *
         * @return
         * The key
         */
        @JsonProperty("key")
        public String getKey() {
            return key;
        }

        /**
         *
         * @param key
         * The key
         */
        @JsonProperty("key")
        public void setKey(String key) {
            this.key = key;
        }

        @JsonAnyGetter
        public Map<String, Object> getAdditionalProperties() {
            return this.additionalProperties;
        }

        @JsonAnySetter
        public void setAdditionalProperty(String name, Object value) {
            this.additionalProperties.put(name, value);
        }
    }
我得到一个例外:

    D/OkHttp: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.shreybank.shrey.pojo.ShreyPojo out of START_ARRAY token
D/OkHttp:  at [Source: [{"_id":"574f1840d21303358a03d78b","name":"Bank One of Kerala","period_from":"2016-03-12T00:00:00.000Z","period_to":"2016-03-12T00:00:00.000Z","status":true,"ins":"Get an account at Bank One of Kerala :P","fields":[{"field":"Account Number","key":"acc"},{"field":"IFSC","key":"ifsc"},{"field":"Branch","key":"branch"},{"field":"Name","key":"name"},{"field":"PAN","key":"pan"}]},{"_id":"574f18e4d21303358a03d78c","name":"Bank Two of India","period_from":"2016-03-12T00:00:00.000Z","period_to":"2016-03-12T00:00:00.000Z","status":true,"ins":"Get an account at Bank Two of India","fields":[{"field":"Account Number","key":"acc"},{"field":"IFSC","key":"ifsc"},{"field":"Branch","key":"branch"},{"field":"Name","key":"name"}]}]; line: 1, column: 1]
D/OkHttp:     at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:873)
D/OkHttp:     at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:869)
D/OkHttp:     at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromArray(BeanDeserializerBase.java:1293)
D/OkHttp:     at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:159)
D/OkHttp:     at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:135)
D/OkHttp:     at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3789)
D/OkHttp:     at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2779)
D/OkHttp:     at com.shreybank.shrey.activities.BankDetailsActivity$1.onResponse(BankDetailsActivity.java:77)
D/OkHttp:     at okhttp3.RealCall$AsyncCall.execute(RealCall.java:133)
D/OkHttp:     at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
D/OkHttp:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
D/OkHttp:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
D/OkHttp:     at java.lang.Thread.run(Thread.java:818)
由于存在类似的问题,我尝试了它们,以防我犯了一个常见错误,因为这一个不一样,如何修复它?

尝试下面的代码

 try {

       JSONArray  jsonArray = new JSONArray(res);
       List<ShreyPojo> pojoList = new ArrayList<>();

       for(int i = 0; i < jsonArray.length(); i++)
       {
          //JSONObject jsonObject = jsonArray.getJSONObject(i);
            String jsonObject = jsonArray.getString(i);

          ShreyPojo obj = mapper.readValue(jsonObject,ShreyPojo.class); 

           pojoList.add(obj);
       }

   // Use ShreyPojo objects as per requirement

 } catch (Exception e)
 {
    Log.w("Exception = ","" + e.toString());
 }
试试看{
JSONArray JSONArray=新JSONArray(res);
List pojoList=new ArrayList();
for(int i=0;iShreyPojo obj=mapper.readValue(jsonObject,ShreyPojo.class);
pojoList.add(obj);
}
//按照要求使用ShreyPojo对象
}捕获(例外e)
{
Log.w(“Exception=”,“+e.toString());
}

Shaheen,res是JSONObject的JSONArray,所以您需要首先从列表中获取所有JSONObject,然后像
List listShreyPojo=objectMapper.readValue(res,new TypeReference(){})一样迭代它啊:D,好像我犯了一个大错,为什么不把它作为一个答案,来结束这个问题呢!如果仍然面临相同的问题,请显示更新日志和代码。ShreyPojo obj=mapper.readValue(jsonObject,ShreyPojo.class);此行出错:无法解析方法“readValue(org.json.JSONObject,java.lang.Class)”,感谢您的帮助!随时享受你的编码
 try {

       JSONArray  jsonArray = new JSONArray(res);
       List<ShreyPojo> pojoList = new ArrayList<>();

       for(int i = 0; i < jsonArray.length(); i++)
       {
          //JSONObject jsonObject = jsonArray.getJSONObject(i);
            String jsonObject = jsonArray.getString(i);

          ShreyPojo obj = mapper.readValue(jsonObject,ShreyPojo.class); 

           pojoList.add(obj);
       }

   // Use ShreyPojo objects as per requirement

 } catch (Exception e)
 {
    Log.w("Exception = ","" + e.toString());
 }