{"data":"{/"message/":/"/",/"result/":0,/"data/":{/"cardtype/":0,/"sealimgs/":[/"{
///"id///":16,///"img///":///"H4znOvV8Cb101BRlW6WhIU8n8B6GEqjM44AAA=/",/"realnameauthstate/":3,/"country/":0,/"faceacstate/":0,/"email/":/"[email protected]/",/"dateformat/":0,/"signimgtype/":/"png/",/"company/":/"/",/"realname/":/"张三/",/"uafacstate/":0}}","mac":"f17d652444ce014c0c9cc58aff0b79bdffbe90b4"}
最近在做一个项目,用的了阿里的fastjson,但是返回的json串中有许多反斜杠,都要被他烦死了(///),查了半天,终于解决了,具体方法如下:
import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson.JSONObject;
public class Main {
public static void main(String[] argv) throws Exception {
List<A> a = new ArrayList<A>() ;
a.add(new A("1", 1)) ;
a.add(new A("2", 2)) ;
a.add(new A("3", 3)) ;
//直接返回对象就不会有反斜杠。。
Object o = JSONObject.toJSON(a) ;
System.out.println(o) ;
}
}
class A{
String name ;
Integer age ;
A(String name , Integer age){
this.name = name ;
this.age = age ;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
直接返回对象就不会有反斜杠了,我的程序是这样的
JSONArray sealimgs = new JSONArray(userImg_seals.size());
int i = 0;
for(UserImg userImgSub : userImg_seals){
byte[] imgdata = userImgSub.getImgdata();
byte[] comp_imgdata = CompressUtil.compressData(imgdata);
sealimg = Base64.encodeBytes(comp_imgdata);
sealimgname = userImgSub.getImgname();
if (StrTool.strNotNull(sealimgname)) {
sealimgname = sealimgname.substring(sealimgname.lastIndexOf(".") + 1, sealimgname.length());
}
JSONObject subJson = new JSONObject();
subJson.put("id", userImgSub.getImgid());
subJson.put("type", sealimgname);
subJson.put("img", sealimg);
sealimgs.add(subJson);
i++;
}
Object obj = JSONObject.toJSON(sealimgs);
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/17677.html