UnrecognizedPropertyException:无法识别的字段"代码"在Android中

时间:2017-04-18 06:47:44

标签: android json objectmapper

我想通过使用ObjectMapper从Jsonutil代码返回一个值,但数据不反映

我正在使用模型,我从中反映了一个描述值,并希望在Spinner中添加

我遇到了这个错误:

 V/Status code: 201
04-18 01:56:01.666 7130-7799/vems.visioneering.com.vems V/RESPONSE SERVER COMM :: [{"primaryKey":1,"code":"C1","description":"Class I","isActive":true},{"primaryKey":2,"code":"C2","description":"Class II","isActive":true},{"primaryKey":3,"code":"C3","description":"Class III","isActive":true},{"primaryKey":4,"code":"C4","description":"Class IV","isActive":true},{"primaryKey":5,"code":"C5","description":"Class V","isActive":true},{"primaryKey":6,"code":"C6","description":"Class VI","isActive":true},{"primaryKey":7,"code":"C7","description":"Class VII","isActive":true},{"primaryKey":8,"code":"C8","description":"Class VIII","isActive":true},{"primaryKey":9,"code":"C9","description":"Class IX","isActive":true},{"primaryKey":10,"code":"C10","description":"Class X","isActive":true},{"primaryKey":11,"code":"C11","description":"Class XI","isActive":true},{"primaryKey":12,"code":"C12","description":"Class XII","isActive":true}]
04-18 01:56:01.666 7130-7130/vems.visioneering.com.vems V/Util Data: {"primaryKey":1,"code":"C1","description":"Class I","isActive":true}
04-18 01:56:01.667 7130-7130/vems.visioneering.com.vems W/System.err: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "code" (class vems.visioneering.com.vems.Model.LKClassData), not marked as ignorable (2 known properties: "description", "primaryKey"])
04-18 01:56:01.667 7130-7130/vems.visioneering.com.vems W/System.err:     at [Source: {"primaryKey":1,"code":"C1","description":"Class I","isActive":true}; line: 1, column: 25] (through reference chain: vems.visioneering.com.vems.Model.LKClassData["code"])
04-18 01:56:01.667 7130-7130/vems.visioneering.com.vems W/System.err:     at com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:817)
04-18 01:56:01.667 7130-7130/vems.visioneering.com.vems W/System.err:     at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:958)
04-18 01:56:01.667 7130-7130/vems.visioneering.com.vems W/System.err:     at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1324)
04-18 01:56:01.667 7130-7130/vems.visioneering.com.vems W/System.err:     at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1302)
04-18 01:56:01.667 7130-7130/vems.visioneering.com.vems W/System.err:     at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:249)
04-18 01:56:01.667 7130-7130/vems.visioneering.com.vems W/System.err:     at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:136)
04-18 01:56:01.667 7130-7130/vems.visioneering.com.vems W/System.err:     at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3562)
04-18 01:56:01.667 7130-7130/vems.visioneering.com.vems W/System.err:     at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2578)
04-18 01:56:01.667 7130-7130/vems.visioneering.com.vems W/System.err:     at vems.visioneering.com.vems.utils.JSonUtil.jsonToObject(JSonUtil.java:86)
04-18 01:56:01.667 7130-7130/vems.visioneering.com.vems W/System.err:     at vems.visioneering.com.vems.fragments.AttendanceFragment.onCreateView(AttendanceFragment.java:94)

我的Java代码用于获取Spinner数据:

try{
             JSONObject msgJSON = new JSONObject();
             msgJSON.put("objName", "LKClass");
             SharedPreferences sharedPref = context.getSharedPreferences
                     (ApplicationUtils.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
             if (ApplicationUtils.BASE_URL == null) {
                 String response = VEMSServices.getInstance(context).getResponseData(msgJSON.toString(), ApplicationUtils.GET_OBJECT);
                 JSONArray jsonArray = new JSONArray(response);
                 final ArrayList<LKClassData> spn = new ArrayList<>();
                 for (int i = 0; i < jsonArray.length(); i++) {
                     spn.add((LKClassData) JSonUtil.jsonToObject(jsonArray.getString(i), LKClassData.class));

                     Log.v("Check Value",spn.toString());
                     //JSONObject jsonObject = jsonArray.getJSONObject(i);
                     //spn.add(new LKClassData(jsonObject.getString("description"), jsonObject.getString("primaryKey")));
                 }
                 vSpinner = new VisioneeringSpinner(context);
                 vSpinner.getSpinner(R.id.spinner_stu_Attendance_Class, spn, this.getActivity());

我的JsonUtil类代码是:

 public static Object jsonToObject(String jsonData, Class<?> clazz) {
        Log.v("Util Data",jsonData);
        try {
            return mapperObj.readValue(jsonData, clazz);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

我的模型类数据:

public class LKClassData {


    public String description;
    public String primaryKey;

    @Override
    public String toString() {
        return description;
    }
}

1 个答案:

答案 0 :(得分:3)

尝试将@JsonIgnoreProperties(ignoreUnknown = true)添加到课程LKClassData

@JsonIgnoreProperties(ignoreUnknown = true)
public class LKClassData {

    public String description;
    public String primaryKey;

    @Override
    public String toString() {
        return description;
    }
}