问题描述
我想我的JSON数据转换为杰克逊使用一个POJO对象。这里是在MainActivity和我的POJO类code。我基本上得到一个JsonMappingException错误。我还附上了整个日志。
MainActivity.java:
ObjectMapper映射器=新ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
尝试{
清单<&条目GT;结果= mapper.readValue(新URL(http://collegewires.com/android/jacksoncw.json),新TypeReference<名单,LT;条目>>(){});
的for(int i = 0; I< results.size(); ++ I){
的System.out.println(results.get(ⅰ).getName());
OUT = OUT +##+ results.get(I).getName();
txt.setText(1111);
}
}赶上(JsonParseException E){
// TODO自动生成catch块
e.printStackTrace(); }赶上(JsonMappingException E){
// TODO自动生成catch块
e.printStackTrace(); }赶上(MalformedURLException的E){
// TODO自动生成catch块
e.printStackTrace(); }赶上(IOException异常五){
// TODO自动生成catch块
e.printStackTrace();
}
Entries.java(这是POJO)
包com.collegewires.jackson;公共类条目{
公共条目(){
}私人字符串ID,姓名,电子邮件地址,性别;
私人手机电话;公用电话getPhone(){
回电话;
}公共无效setPhone(电话电话){
this.phone =电话;
}公共字符串的getId(){
返回ID;
}公共无效SETID(字符串ID){
this.id = ID;
}公共字符串的getName(){
返回名称;
}公共无效setname可以(字符串名称){
this.name =名称;
}公共字符串getEmail(){
返回的电子邮件;
}公共无效setEmail(字符串email){
this.email =电子邮件;
}公共字符串的getAddress(){
退货地址;
}公共无效setAddress(字符串地址){
this.address =地址;
}公共字符串getGender(){
返回性别;
}公共无效setGender(字符串性别){
this.gender =性别;
}@覆盖
公共字符串的toString(){
返回条目:{ID:+身份证+,名称:+姓名+
电子邮件:+电子邮件+,地址:+地址+,性别:+性别+,电话:+手机+};
}公共类电话{
公用电话(){
}
公共字符串手机,家庭,办公;
公共字符串getMobile(){
返回移动;
} 公共无效setMobile(字符串移动){
this.mobile =移动;
} 公共字符串getHome(){
回家;
} 公共无效设为首页(字符串家){
this.home =回家;
} 公共字符串getOffice(){
返回办公室;
} 公共无效setOffice(字符串办){
this.office =办事处;
} @覆盖
公共字符串的toString(){
返回电话:{移动+移动+,回家+家居+
办公室:+办公+};
}
}
}
现在,我得到我的日志中出现以下错误。正因为如此,我无法继续我的工作继续进行。这里是日志:
12月7日至12日:06:13.206:W / System.err的(30593):com.fasterxml.jackson.databind.JsonMappingException:无法反序列化的java.util.ArrayList实例出来START_OBJECT令牌
12月7日至12日:06:13.206:W / System.err的(30593):在[来源:http://collegewires.com/android/jacksoncw.json;行:1,柱:1]
12月7日至12日:06:13.211:W / System.err的(30593):在com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:575)
12月7日至12日:06:13.211:W / System.err的(30593):在com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:569)
12月7日至12日:06:13.211:W / System.err的(30593):在com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.handleNonArray(CollectionDeserializer.java:259)
12月7日至12日:06:13.211:W / System.err的(30593):在com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:217)
12月7日至12日:06:13.211:W / System.err的(30593):在com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:207)
12月7日至12日:06:13.211:W / System.err的(30593):在com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:23)
12月7日至12日:06:13.211:W / System.err的(30593):在com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
12月7日至12日:06:13.211:W / System.err的(30593):在com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2018)
12月7日至12日:06:13.211:W / System.err的(30593):在com.collegewires.jackson.MainActivity.onCreate(MainActivity.java:81)
12月7日至12日:06:13.211:W / System.err的(30593):在android.app.Activity.performCreate(Activity.java:5250)
12月7日至12日:06:13.211:W / System.err的(30593):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
12月7日至12日:06:13.211:W / System.err的(30593):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
12月7日至12日:06:13.211:W / System.err的(30593):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297)
12月7日至12日:06:13.211:W / System.err的(30593):在android.app.ActivityThread.access $ 700(ActivityThread.java:152)
12月7日至12日:06:13.211:W / System.err的(30593):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1282)
12月7日至12日:06:13.211:W / System.err的(30593):在android.os.Handler.dispatchMessage(Handler.java:99)
12月7日至12日:06:13.211:W / System.err的(30593):在android.os.Looper.loop(Looper.java:137)
12月7日至12日:06:13.211:W / System.err的(30593):在android.app.ActivityThread.main(ActivityThread.java:5328)
12月7日至12日:06:13.211:W / System.err的(30593):在java.lang.reflect.Method.invokeNative(本机方法)
12月7日至12日:06:13.211:W / System.err的(30593):在java.lang.reflect.Method.invoke(Method.java:511)
12月7日至12日:06:13.211:W / System.err的(30593):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1102)
12月7日至12日:06:13.211:W / System.err的(30593):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
12月7日至12日:06:13.211:W / System.err的(30593):在dalvik.system.NativeStart.main(本机方法)
12月7日至12日:06:13.251:D / libEGL(30593):加载/vendor/lib/egl/libEGL_POWERVR_SGX544_115.so
12月7日至12日:06:13.261:D / libEGL(30593):加载/vendor/lib/egl/libGLESv1_CM_POWERVR_SGX544_115.so
12月7日至12日:06:13.266:D / libEGL(30593):加载/vendor/lib/egl/libGLESv2_POWERVR_SGX544_115.so
12月7日至12日:06:132.71:I /(30593):@IMGSRV:日期:2013年4月23日(11时19分33秒)|! BUILDER:LSI3D |修订:e9cd1377d8225457ae40
12月7日至12日:06:13.346:D / OpenGLRenderer(30593):启用调试模式0
12月7日至12日:06:31.521:W / IInputConnectionWrapper(30593)上的非活动InputConnection setComposingText
-
在项&放取出构造函数; 电话
-
GetEntries结果= mapper.readValue(新URL(http://collegewires.com/android/jacksoncw.json),GetEntries.class);
-
条目似乎是在你的JSON的参数。
GetEntries.java
包com.collegewires.jackson;进口的java.util.List;公共类GetEntries {
私人列表<&条目GT;条目; 公开名单<&条目GT; getEntries(){
返回this.entries;
}
公共无效setEntries(列表<&条目GT;项){
this.entries =条目;
}
}
Entries.java
包com.collegewires.jackson;进口的java.util.List;公共类条目{
私人字符串的地址;
私人字符串电子邮件;
私人字符串性别;
私人字符串ID;
私人字符串名称;
私人手机电话; 公共字符串的getAddress(){
返回this.address;
}
公共无效setAddress(字符串地址){
this.address =地址;
}
公共字符串getEmail(){
返回this.email;
}
公共无效setEmail(字符串email){
this.email =电子邮件;
}
公共字符串getGender(){
返回this.gender;
}
公共无效setGender(字符串性别){
this.gender =性别;
}
公共字符串的getId(){
返回this.id;
}
公共无效SETID(字符串ID){
this.id = ID;
}
公共字符串的getName(){
返回this.name;
}
公共无效setname可以(字符串名称){
this.name =名称;
}
公用电话getPhone(){
返回this.phone;
}
公共无效setPhone(电话电话){
this.phone =电话;
}
}
Phone.java
包com.collegewires.jackson;进口的java.util.List;公共类电话{
私人字符串回家;
私人字符串移动;
私人字符串办事处; 公共字符串getHome(){
返回this.home;
}
公共无效设为首页(字符串家){
this.home =回家;
}
公共字符串getMobile(){
返回this.mobile;
}
公共无效setMobile(字符串移动){
this.mobile =移动;
}
公共字符串getOffice(){
返回this.office;
}
公共无效setOffice(字符串办){
this.office =办事处;
}
}
I'm trying to convert my json data to a POJO object using Jackson. Here is the MainActivity and my POJO class code. I'm basically getting a JsonMappingException error. I have also attached the whole log.
MainActivity.java :
ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
try {
List<Entries> results = mapper.readValue(new URL("http://collegewires.com/android/jacksoncw.json"), new TypeReference<List<Entries>>(){});
for(int i=0;i<results.size();++i){
System.out.println(results.get(i).getName());
out = out + " ## " + results.get(i).getName();
txt.setText("1111");
}
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Entries.java (this is the POJO)
package com.collegewires.jackson;
public class Entries {
public Entries(){
}
private String id, name, email, address, gender;
private Phone phone;
public Phone getPhone() {
return phone;
}
public void setPhone(Phone phone) {
this.phone = phone;
}
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 getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Entries:{id: " + id + ", name: " + name +
", email: " + email + ", address: " + address + ", gender: " + gender + ", phone: " + phone + "}";
}
public class Phone {
public Phone(){
}
public String mobile, home, office;
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getHome() {
return home;
}
public void setHome(String home) {
this.home = home;
}
public String getOffice() {
return office;
}
public void setOffice(String office) {
this.office = office;
}
@Override
public String toString() {
return "Phone:{mobile: " + mobile + ", home: " + home +
", office: " + office + "}";
}
}
}
Now, I'm getting the following error in my log. Because of this, I'm unable to proceed with my work. Here is the log :
07-12 12:06:13.206: W/System.err(30593):
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
07-12 12:06:13.206: W/System.err(30593): at [Source: http://collegewires.com/android/jacksoncw.json; line: 1, column: 1]
07-12 12:06:13.211: W/System.err(30593): at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:575)
07-12 12:06:13.211: W/System.err(30593): at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:569)
07-12 12:06:13.211: W/System.err(30593): at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.handleNonArray(CollectionDeserializer.java:259)
07-12 12:06:13.211: W/System.err(30593): at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:217)
07-12 12:06:13.211: W/System.err(30593): at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:207)
07-12 12:06:13.211: W/System.err(30593): at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:23)
07-12 12:06:13.211: W/System.err(30593): at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
07-12 12:06:13.211: W/System.err(30593): at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2018)
07-12 12:06:13.211: W/System.err(30593): at com.collegewires.jackson.MainActivity.onCreate(MainActivity.java:81)
07-12 12:06:13.211: W/System.err(30593): at android.app.Activity.performCreate(Activity.java:5250)
07-12 12:06:13.211: W/System.err(30593): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
07-12 12:06:13.211: W/System.err(30593): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
07-12 12:06:13.211: W/System.err(30593): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297)
07-12 12:06:13.211: W/System.err(30593): at android.app.ActivityThread.access$700(ActivityThread.java:152)
07-12 12:06:13.211: W/System.err(30593): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
07-12 12:06:13.211: W/System.err(30593): at android.os.Handler.dispatchMessage(Handler.java:99)
07-12 12:06:13.211: W/System.err(30593): at android.os.Looper.loop(Looper.java:137)
07-12 12:06:13.211: W/System.err(30593): at android.app.ActivityThread.main(ActivityThread.java:5328)
07-12 12:06:13.211: W/System.err(30593): at java.lang.reflect.Method.invokeNative(Native Method)
07-12 12:06:13.211: W/System.err(30593): at java.lang.reflect.Method.invoke(Method.java:511)
07-12 12:06:13.211: W/System.err(30593): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
07-12 12:06:13.211: W/System.err(30593): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
07-12 12:06:13.211: W/System.err(30593): at dalvik.system.NativeStart.main(Native Method)
07-12 12:06:13.251: D/libEGL(30593): loaded /vendor/lib/egl/libEGL_POWERVR_SGX544_115.so
07-12 12:06:13.261: D/libEGL(30593): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX544_115.so
07-12 12:06:13.266: D/libEGL(30593): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX544_115.so
07-12 12:06:13.271: I/(30593): !@IMGSRV: DATE: 2013.04.23(11:19:33) | BUILDER: LSI3D | REVISION: e9cd1377d8225457ae40
07-12 12:06:13.346: D/OpenGLRenderer(30593): Enabling debug mode 0
07-12 12:06:31.521: W/IInputConnectionWrapper(30593): setComposingText on inactive InputConnection
Remove the Constructor in Entries & Phone
GetEntries results = mapper.readValue(new URL("http://collegewires.com/android/jacksoncw.json"), GetEntries.class);
Entries seems to be a parameter in your JSON.
GetEntries.java
package com.collegewires.jackson;
import java.util.List;
public class GetEntries{
private List<Entries> entries;
public List<Entries> getEntries(){
return this.entries;
}
public void setEntries(List<Entries> entries){
this.entries = entries;
}
}
Entries.java
package com.collegewires.jackson;
import java.util.List;
public class Entries{
private String address;
private String email;
private String gender;
private String id;
private String name;
private Phone phone;
public String getAddress(){
return this.address;
}
public void setAddress(String address){
this.address = address;
}
public String getEmail(){
return this.email;
}
public void setEmail(String email){
this.email = email;
}
public String getGender(){
return this.gender;
}
public void setGender(String gender){
this.gender = gender;
}
public String getId(){
return this.id;
}
public void setId(String id){
this.id = id;
}
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
public Phone getPhone(){
return this.phone;
}
public void setPhone(Phone phone){
this.phone = phone;
}
}
Phone.java
package com.collegewires.jackson;
import java.util.List;
public class Phone{
private String home;
private String mobile;
private String office;
public String getHome(){
return this.home;
}
public void setHome(String home){
this.home = home;
}
public String getMobile(){
return this.mobile;
}
public void setMobile(String mobile){
this.mobile = mobile;
}
public String getOffice(){
return this.office;
}
public void setOffice(String office){
this.office = office;
}
}
这篇关于杰克逊JsonMappingException:无法反序列化实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!