我似乎无法将JSON数组转换为Java中的Map

时间:2016-10-12 17:31:08

标签: java android json jackson

我正在尝试从资产中读取JSON数据并将其转换为地图。但是,它似乎没有转换,并且每当我运行代码时都会给我错误。

文件位于assets文件夹中,名为“json_database_main.json”

public Map loadMapFromAsset() {
    String json = null;
    Map objects_map = new HashMap();
    Product product;
    Log.d("Started ldmpfass", "Yeah");
    try {
        InputStream is = getAssets().open(file_database);
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        json = new String(buffer, "UTF-8");
        JSONArray jsonArray = new JSONArray(json);
        Log.d("Sumthin", Integer.toString(size));
        for(int i = 0; i < jsonArray.length(); i++)
        {
            JSONObject jsonObj = jsonArray.getJSONObject(i);
            Log.d("JSobject:", jsonObj.toString());
            product = mapper.readValue(jsonObj.toString(), Product.class);
            Log.d("Object:", product.getCode());
            objects_map.put(product.getCode(), product);


        }
    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return objects_map;
}

这是我在logcat中收到的消息:

D/JSobject:: {"code":"0001","description":"Pienas","type":"Pieno produktai"}
W/System.err: com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.matas.checksv3.Product: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)
W/System.err:  at [Source: {"code":"0001","description":"Pienas","type":"Pieno produktai"}; line: 1, column: 2]
W/System.err:     at com.fasterxml.jackson.databind.DeserializationContext.instantiationException(DeserializationContext.java:1456)
W/System.err:     at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(    DeserializationContext.java:1012)
W/System.err:     at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1203)
W/System.err:     at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:314)
W/System.err:     at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:148)
W/System.err:     at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3789)
W/System.err:     at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2833)
W/System.err:     at com.matas.checksv3.MainActivity.loadMapFromAsset(MainActivity.java:152)
W/System.err:     at com.matas.checksv3.MainActivity$2.onClick(MainActivity.java:85)
W/System.err:     at android.view.View.performClick(View.java:5610)
W/System.err:     at android.view.View$PerformClick.run(View.java:22260)
W/System.err:     at android.os.Handler.handleCallback(Handler.java:751)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err:     at android.os.Looper.loop(Looper.java:154)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6077)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
D/AndroidRuntime: Shutting down VM

我知道我的代码很难看,但这是我的第一个Android应用程序,我会努力变得更好,所以指出我可以做得更好的任何事情。

我编辑了我的代码,现在我得到了这个。我不确定产品需要什么类。这是:

public class Product {

    String code;
    String description;
    String type;
    public Product(String code, String description, String type)
    {
        this.code = code;
        this.description = description;
        this.type = type;
    }
    public Product(String code, String description)
    {
        this.code = code;
        this.description = description;
    }
    public String getCode()
    {
        return code;
    }
    public String getType() {
        return type;
    }

    public String getDescription() {
        return description;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setType(String type) {
        this.type = type;
    }
}

编辑:搞定了!已添加:public Product(){}到产品类。

1 个答案:

答案 0 :(得分:0)

  

com.fasterxml.jackson.databind.JsonMappingException:无法构造com.matas.checksv3.Product的实例:找不到合适的构造函数,无法从Object值反序列化(缺少默认构造函数或创建者,或者可能需要添加/启用类型信息?)

将空构造函数添加到数据类中。