安卓 I似乎无法将JSON数组转换为Java中的映射
我试图从资产中读取JSON数据,并将其转换为映射。然而,每当我运行代码时,它似乎不会转换,并给我错误
文件位于资产文件夹中,名为“”json\u database\u 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 安卓.view.View.performClick(View.java:5610)
W/System.err: at 安卓.view.View$PerformClick.run(View.java:22260)
W/System.err: at 安卓.os.Handler.handleCallback(Handler.java:751)
W/System.err: at 安卓.os.Handler.dispatchMessage(Handler.java:95)
W/System.err: at 安卓.os.Looper.loop(Looper.java:154)
W/System.err: at 安卓.app.ActivityThread.main(ActivityThread.java:6077)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
W/System.err: at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:755)
D/AndroidRuntime: Shutting down VM
我知道我的代码很难看,但这是我的第一个安卓应用程序,我会努力做得更好,所以请指出我可以做得更好的地方
我编辑了我的代码,现在我明白了。我不确定产品需要什么等级。这是:
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(){}
到产品类
共 (0) 个答案