我正在尝试在codenameone应用程序中使用mirah进行JSON到POJO映射。当我想映射一个简单的JSON时,它可以工作
{"id":"1","name":"foo","classification":"10"}
有了这堂课:
public class Brand {
private String id;
private String name;
private String classification;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClassification() {
return classification;
}
public void setClassification(String classification) {
this.classification = classification;
}
}
现在我想把它包装在一个Message Object中,我有一个品牌列表:
import java.util.List;
public class Message {
public List<Brand> brands;
public List<Brand> getBrands() {
return brands;
}
public void setBrands(List<Brand> brands) {
this.brands = brands;
}
}
我使用这个Mirah脚本进行映射:
data_mapper Message:MessageMapper
data_mapper Brand:BrandMapper
像shannah描述的那样here。
我的代码,我称之为Webservice:
MessageMapper scheduleMapper = new MessageMapper();
DataMapper.createContext(Arrays.asList(
scheduleMapper,
new BrandMapper()
), new DataMapper.Decorator() {
public void decorate(DataMapper mapper) {
mapper.setReadKeyConversions(Arrays.asList(DataMapper.CONVERSION_NONE));
}
});
try {
Message message = scheduleMapper.readJSONFromURL("http://localhost/php-REST-DigitaleMusterplatte/api.php/brands", Message.class);
System.out.println(message);
} catch (IOException ex) {
Log.e(ex);
}
这是json的回应:
{"brands":[{"id":"1","name":"foo","classification":"10"},{"id":"2","name":"bar","classification":"20"}]}
我得到了这个例外:
java.lang.RuntimeException: Failed to get key brands for class interface java.util.List because it was not a registered object type
at ca.weblite.codename1.mapper.DataMapper.get(DataMapper.java:507)
at com.mycompany.app.dmp.models.MessageMapper.readMap(/Volumes/Windows VMS/Documents/Shared/NetBeansProjects/mirah_macro_utils/MirahMacroUtils/src/ca/weblite/mirah/utils/DataMapperBuilder.mirah)
at ca.weblite.codename1.mapper.DataMapper.readMap(DataMapper.java:719)
at ca.weblite.codename1.mapper.DataMapper.readJSON(DataMapper.java:780)
at ca.weblite.codename1.mapper.DataMapper.readJSON(DataMapper.java:792)
at ca.weblite.codename1.mapper.DataMapper.readJSONFromConnection(DataMapper.java:767)
at ca.weblite.codename1.mapper.DataMapper.readJSONFromURL(DataMapper.java:754)
at com.mycompany.myapp.MyApplication.start(MyApplication.java:96)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.codename1.impl.javase.Executor$1$1.run(Executor.java:123)
at com.codename1.ui.Display.processSerialCalls(Display.java:1152)
at com.codename1.ui.Display.mainEDTLoop(Display.java:969)
at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120)
at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)
演示应用程序OSCONScheduler正常工作。
答案 0 :(得分:1)
这看起来像个错误。但请尝试将brands
更改为private
而不是public
。可能会对是否使用您的访问者/可变或使用公共变量感到困惑。