您的位置:首页 > Web前端 > JavaScript

使用Fastjson生成Json字符串少字段属性(数据丢失)

2016-05-16 10:06 621 查看
在开发中经常要使用到fastJson来转换对象为json 串,但是最近发现在一个对象转换的时候,总是丢失了一个字段的值,(数据丢失).就很纳闷。到网上找了很多方法总是不行,最后总算是在一篇博文中看到问题的关键!现在整理如下,希望可以帮助到更多的人。

package per.eblink.pojo;

public class Node {

private String id;
private String pId;
private String name;
private boolean open;

private Node() {
super();
}

public Node(String id, String pId, String name, boolean open) {
super();
this.id = id;
this.pId = pId;
this.name = name;
this.open = open;
}

public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getpId() {
return pId;
}
public void setpId(String pId) {
this.pId = pId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isOpen() {
return open;
}
public void setOpen(boolean open) {
this.open = open;
}
}


转换类:

<span style="font-size:14px;">package per.eblink.pojo;

public class Node {

private String id;
private String pId;
private String name;
private boolean open;

private Node() {
super();
}

public Node(String id, String pId, String name, boolean open) {
super();
this.id = id;
this.pId = pId;
this.name = name;
this.open = open;
}

public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getpId() {
return pId;
}
public void setpId(String pId) {
this.pId = pId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isOpen() {
return open;
}
public void setOpen(boolean open) {
this.open = open;
}
}</span>


最后是控制台打印生成的结果如下:

FastJson生成字符串是:{"id":"2","name":"节点1","open":true}

Gson生成字符串是:{"id":"2","pId":"1","name":"节点1","open":true}

用FastJson就是少了个属性pId没有被转化出来,用Gson和其他的却可以,而我的Node对象只是个普通的JAVA类而已,麻烦你看一下,谢谢!

答案:1)你的get,set方法估计多半是自动生成的,Fastjson在生成的时候去判断pId有没有对应的get方法是区分了大小写的,所以找不到对应的get方法(getPId())。
2)如果页面上也需要使用node对象,就必须使用自动生成的get、set方法。与1)相互冲突,最根本的解决办法是,不适用第一个单词只有一个小写字母的属性名,换一个属性名字paId。


问题2:fastjson生成json时Null属性不显示

生成JSON代码片段

[java] 预览复制

Map < String , Object > jsonMap = new HashMap< String , Object>();

jsonMap.put("a",1);

jsonMap.put("b","");

jsonMap.put("c",null);

jsonMap.put("d","wuzhuti.cn");

String str = JSONObject.toJSONString(jsonMap);

System.out.println(str);

//输出结果:{"a":1,"b":"",d:"wuzhuti.cn"}

从输出结果可以看出,null对应的key已经被过滤掉;这明显不是我们想要的结果,这时我们就需要用到fastjson的SerializerFeature序列化属性

也就是这个方法:JSONObject.toJSONString(Object object, SerializerFeature... features)


Fastjson的SerializerFeature序列化属性

--来自oschina bfleeee博客

QuoteFieldNames———-输出key时是否使用双引号,默认为true

WriteMapNullValue——–是否输出值为null的字段,默认为false

WriteNullNumberAsZero—-数值字段如果为null,输出为0,而非null

WriteNullListAsEmpty—–List字段如果为null,输出为[],而非null

WriteNullStringAsEmpty—字符类型字段如果为null,输出为”“,而非null

WriteNullBooleanAsFalse–Boolean字段如果为null,输出为false,而非null


代码

[java] 预览复制

Map < String , Object > jsonMap = new HashMap< String , Object>();

jsonMap.put("a",1);

jsonMap.put("b","");

jsonMap.put("c",null);

jsonMap.put("d","wuzhuti.cn");

String str = JSONObject.toJSONString(jsonMap,SerializerFeature.WriteMapNullValue);

System.out.println(str);

//输出结果:{"a":1,"b":"","c":null,"d":"wuzhuti.cn"}

参考文章:

1)fastjson生成json时Null属性不显示:http://www.oschina.net/question/818749_131396

2)使用Fastjson生成Json字符串少字段属性: http://www.oschina.net/question/818749_131396
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: