将json字符串转成对象时,某些对象的属为null值

happy521feng 发布于 2014/06/26 19:55
阅读 3K+
收藏 0
 {
  "patientId" : "02116578",
  "remarkPrivate" : "1",
  "remarkLevel" : "2",
  "name" : "1111111",
  "content" : "33333333333",
  "fileName" : "tmp.zip",
  "info" : "33333333333",
  "doctorUser" : "1466",
  "deptCode" : "1001",
  "sync" : "1",
  "visitId" : "1"
}
fastjson 转对象 是这种写法吗?
RemarkInfo tmpRemarkInfo = JSON.parseObject(aRemarkInfoJson, RemarkInfo.class);
RemarkInfo tmpRemarkInfo = JSONObject.parseObject(aRemarkInfoJson, RemarkInfo.class);
我这个   json 用 fastjson 转对象的时候   sync 转换为null
加载中
1
zqq90
zqq90

Bug在这里,是你的 Bean 的 setter 弄错了

public String getSync() {
    return sync;
}


public void setSynchronous(String sync) {
    this.sync = sync;
}


你越是确信他没错,他越可能是错的

h
happy521feng
这是我后来 改的时候 改错了,我又把sync的setter方法改过来之后 ,sync 还是null值,我用jackson是可以的
0
若水191
若水191
sync可能是关键字,换一个试试
zqq90
zqq90
FastJson 原则上不会做这种限制,一个有效的Java命名规范,不会在 Fastjson 里面被作为关键字
h
happy521feng
有可能吧,即便是也是fastjson的,不是java的
0
zqq90
zqq90
更多的可能是你的 RemarkInfo 有问题,为啥不贴一下RemarkInfo
h
happy521feng
我在下面的回复种贴出来了,你看一下,sync 本来就是字符串类型
h
happy521feng
我在下面的回复种贴出来了,你看一下,但我觉得RemarkInfo类没什么问题呀
0
红星xx
红星xx

你的数据类型不对 ,根据json规范 ,"sync" : "1", 代表的是 sync是 一个字符串 1  , "sync" : 1, 才是 数字1  ,双引号是关键。具体可以看:

http://baike.baidu.com/view/136475.htm

JSON 值 JSON 值可以是:
数字(整数或浮点数)
字符串(在双引号中)
逻辑值(true 或 false)
数组(在方括号中)
对象(在花括号中)
null


zqq90
zqq90
如果按照你说的 应该抛异常
h
happy521feng
我在下面的回复种贴出来了,你看一下,sync 本来就是字符串类型,所以我觉得 不是类型的问题
0
h
happy521feng

public class RemarkInfo {

// 备注id

private String remarkId;

// 用户编码

private String doctorUser;

// 备注级别

private String remarkLevel;

// 备注私有

private String remarkPrivate;

// 备注名称

private String name;

// 同步

private String sync;

// 病人编号

private String patientId;

// 就诊次数

private int visitId;

// 科室编码

private String deptCode;

// 备注内容

private String content;

// 布局html内容

private String layoutHtml;

// 简明信息

private String info;

private String fileName;


public String getRemarkId() {

return remarkId;

}


public void setRemarkId(String remarkId) {

this.remarkId = remarkId;

}


public String getDoctorUser() {

return doctorUser;

}


public void setDoctorUser(String doctorUser) {

this.doctorUser = doctorUser;

}


public String getRemarkLevel() {

return remarkLevel;

}


public void setRemarkLevel(String remarkLevel) {

this.remarkLevel = remarkLevel;

}


public String getRemarkPrivate() {

return remarkPrivate;

}


public void setRemarkPrivate(String remarkPrivate) {

this.remarkPrivate = remarkPrivate;

}


public String getName() {

return name;

}


public void setName(String name) {

this.name = name;

}


public String getSync() {

return sync;

}


public void setSynchronous(String sync) {

this.sync = sync;

}


public String getPatientId() {

return patientId;

}


public void setPatientId(String patientId) {

this.patientId = patientId;

}


public int getVisitId() {

return visitId;

}


public void setVisitId(int visitId) {

this.visitId = visitId;

}


public String getDeptCode() {

return deptCode;

}


public void setDeptCode(String deptCode) {

this.deptCode = deptCode;

}


public String getContent() {

return content;

}


public void setContent(String content) {

this.content = content;

}


public String getLayoutHtml() {

return layoutHtml;

}


public void setLayoutHtml(String layoutHtml) {

this.layoutHtml = layoutHtml;

}


public String getInfo() {

return info;

}


public void setInfo(String info) {

this.info = info;

}


public String getFileName() {

return fileName;

}


public void setFileName(String fileName) {

this.fileName = fileName;

}

}

0
h
happy521feng

public class RemarkInfo {

// 备注id

private String remarkId;

// 用户编码

private String doctorUser;

// 备注级别

private String remarkLevel;

// 备注私有

private String remarkPrivate;

// 备注名称

private String name;

// 同步

private String sync;

// 病人编号

private String patientId;

// 就诊次数

private int visitId;

// 科室编码

private String deptCode;

// 备注内容

private String content;

// 布局html内容

private String layoutHtml;

// 简明信息

private String info;

private String fileName;

public String getRemarkId() {

return remarkId;

}

public void setRemarkId(String remarkId) {

this.remarkId = remarkId;

}

public String getDoctorUser() {

return doctorUser;

}

public void setDoctorUser(String doctorUser) {

this.doctorUser = doctorUser;

}

public String getRemarkLevel() {

return remarkLevel;

}

public void setRemarkLevel(String remarkLevel) {

this.remarkLevel = remarkLevel;

}

public String getRemarkPrivate() {

return remarkPrivate;

}

public void setRemarkPrivate(String remarkPrivate) {

this.remarkPrivate = remarkPrivate;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getSync() {

return sync;

}

public void setSync(String sync) {

this.sync = sync;

}

public String getPatientId() {

return patientId;

}

public void setPatientId(String patientId) {

this.patientId = patientId;

}

public int getVisitId() {

return visitId;

}

public void setVisitId(int visitId) {

this.visitId = visitId;

}

public String getDeptCode() {

return deptCode;

}

public void setDeptCode(String deptCode) {

this.deptCode = deptCode;

}

public String getContent() {

return content;

}

public void setContent(String content) {

this.content = content;

}

public String getLayoutHtml() {

return layoutHtml;

}

public void setLayoutHtml(String layoutHtml) {

this.layoutHtml = layoutHtml;

}

public String getInfo() {

return info;

}

public void setInfo(String info) {

this.info = info;

}

public String getFileName() {

return fileName;

}

public void setFileName(String fileName) {

this.fileName = fileName;

}

0
xuzhou2013
xuzhou2013

不明白你的问题, 我测试一下, 是ok的.

System.out.println(jsonString);
		RemarkInfo remarkInfo=JSON.parseObject(jsonString, RemarkInfo.class);
		System.out.println(remarkInfo);



0
xuzhou2013
xuzhou2013

结果如下:


  "patientId" : "02116578", 
  "remarkPrivate" : "1", 
  "remarkLevel" : "2", 
  "name" : "1111111", 
  "content" : "33333333333", 
  "fileName" : "tmp.zip", 
  "info" : "33333333333", 
  "doctorUser" : "1466", 
  "deptCode" : "1001", 
  "sync" : "1", 
  "visitId" : "1" 
}
test.java.com.oreilly.zhou.fastjson.RemarkInfo@1add2dd[remarkId=<null>,doctorUser=1466,remarkLevel=2,remarkPrivate=1,name=1111111,sync=1,patientId=02116578,visitId=1,deptCode=1001,content=33333333333,layoutHtml=<null>,info=33333333333,fileName=tmp.zip]


h
happy521feng
哥们儿 你用的fastjson 哪个版本,我用的1.41和1.40
返回顶部
顶部