JSON数据格式转换(StringBuffer拼接json串)⼤全及⽤法JSON是基于{“键”:“值”} 对的存在,当然我们也可以多层嵌套,对于刚刚学习JSON⼗分便捷⽽且很好⽤,很容理解。话不多说直接上代码: public String queryPostInterface(String theNewInterface) throws Exception {
JSONObject jsonObject = new JSONObject(theNewInterface);
String postId = AESUtil.String("post_id"), cKey);//帖⼦ID
StringBuffer sb = new StringBuffer();
int size = 0;//定义⼀个变量⽤来接收循环多少次(共多少条数据)
if(theNewInterface!=null && !"".equals(theNewInterface)){
if (postId != null && !"".equals(postId)) {
//获取帖⼦信息 tieba(具体业务⾃⼰查询这⾥只是个例⼦)
TyPostInfo postInfo = tyPostBarService.selpostInfoById(Long.valueOf(postId));
//查询帖⼦回复信息(具体业务⾃⼰查询这⾥只是个例⼦)(<TyPostbarReply>泛型是个对象)object to
List<TyPostbarReply> replies = tyPostBarService.selectHuiHuid(Long.valueOf(postId));
if (replies != null) {
sb.append("{\"stateCode\": " + 1 + ","); //JSON串的开头信息
sb.append(" \"message\": \"成功\",");
sb.append("\"replayList\": [");//JSON结果集
for (TyPostbarReply reply : replies) { //循环结果集,进⾏拼接
//获取⽤户ID uuid
Long userId = ReplyUserid().toString()); //select UO.updateUserId from USER_INFO UO where id=?
UserInfoVo usesr = tyPostBarService.selectById_yb(userId);
String photo = "";
if (UserPhoto() != null) {
photo = UserPhoto().substring(0, UserPhoto().lastIndexOf("."));
} else {
photo = "";
}
sb.append("{\"userPhoto\": \"" + String() + "\",");//⽤户照⽚
sb.append("\"userName\": \"" + RealName().toString() + "\",");//⽤户姓名
sb.append("\"floor\": \"" + FloorNum().toString() + "\",");//楼层数
sb.append("\"barID\": \"" + PostBarId().toString() + "\",");//贴吧Id
sb.append("\"postID\": \"" + Id().toString() + "\",");//帖⼦id
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sb.append("\"createTime\": \"" + sdf.CreateTime()) + "\",");//创建时间
sb.append("\"content\": \"" + ReplyContent().toString() + "\"");//评论内容
size = size + 1;//循环⼀次+1
if (size < replies.size()) {//这⾥需要注意下。如果循环总条数⼩于查出来的总条数每次循环完毕⼀次都在 “ },” 加上“,” 否则不加 “,”
sb.append("},");
} else {
sb.append("}");
}
}
sb.append("]}"); //最后在拼接最外层(在循环外部)
}
} else {
sb.append("{\"stateCode\":" + 0 + ",");
sb.append("\"message\":\" 传⼊参数为空\"}");
}
}else{
sb.append("{\"stateCode\":" + 0 + ",");
sb.append("\"message\":\" 传⼊参数为空\"}");
}
return AESUtil.String(), cKey);
}
}
最后就会拼接成JSON串,具体业务具体分析,这只是⼀个⽅发,⼀个思想,编程重在思想
解析和JSON基本⽤法:wwwblogs/ysySelf/p/10985410.html
以上是最基本的拼接⽅式下⾯看下json⽤法:
1:创建JSONObject,添加属性
//创建JSONObject
JSONObject json = new JSONObject();
//添加属性
json.put("username", "张三");
json.put("password", "123");
//打印
System.out.println(json);
//增加属性
json.element("sex", "男");
json.put("age", 18);
System.out.println(json);
根据key返回输出:System.out.("sex"));
2:判断输出对象的类型
boolean isArray = json.isArray(); //是否是数组
boolean isEmpty = json.isEmpty(); // 是否为空
boolean isNullObject = json.isNullObject(); // 是否为空对象
System.out.println("是否数组:"+isArray+", 是否空:"+isEmpty+", 是否空为空对象:"+isNullObject); 3:把JSONArray添加到JSONObject中
JSONArray jsonArray = new JSONArray();
jsonArray.add(0, "张三");
jsonArray.add(1, "123");
//开始添加
json.element("student", jsonArray);
System.out.println(json);
详细代码如下:
//创建JSONObject
JSONObject json = new JSONObject();
//添加属性
json.put("username", "张三");
json.put("password", "123");
//打印
System.out.println(json);
//增加属性
json.element("sex", "男");
json.put("age", 18);
System.out.println(json);
//根据key返回
System.out.("sex"));
//判断输出对象的类型
boolean isArray = json.isArray();
boolean isEmpty = json.isEmpty();
boolean isNullObject = json.isNullObject();
System.out.println("是否数组:"+isArray+", 是否空:"+isEmpty+", 是否空为空对象:"+isNullObject); System.out.println("=====");
//把JSONArray添加到JSONObject中
JSONArray jsonArray = new JSONArray();
jsonArray.add(0, "张三");
jsonArray.add(1, "123");
//开始添加
json.element("student", jsonArray);
System.out.println(json);
JSONArray:
public static void main(String[] args) {
//创建JSONArray
JSONArray jsonArray = new JSONArray();
//添加
jsonArray.add(0, "张三");
jsonArray.add(1, "123");
jsonArray.element("男");
System.out.println(jsonArray);
//根据下标返回输出
System.out.(0));
//根据下标设置新值,修改
jsonArray.set(0, "李四");
System.out.println(jsonArray);
/
/把JSONObject放⼊到JSONArray中
JSONObject jsonObject = new JSONObject(); jsonObject.put("username", "张三");
jsonObject.put("password", "123");
jsonArray.add(jsonObject);
System.out.println(jsonArray);
//循环输出
for(int i = 0; i < jsonArray.size(); i++) {
System.out.(i));
}
}
JavaBean与json字符串互转
public class Student{
private String username;
private String password;
//get set
}
定义对象,JavaBean(对象)对象转json字符串
//定义对象
Student stu = new Student("张三", "123456");
//JavaBean对象转json字符串
JSONObject jsonObject = JSONObject.fromObject(stu);
System.out.println(jsonObject);
json字符串转为javaBean 关键字 toBean();
//json字符串转为javaBean
//定义json字符串
String jsondata = "{\"username\":\"李四\", \"password\":\"123\"}";
/
/转为json对象
JSONObject json = JSONObject.fromObject(jsondata);
//转为JavaBean对象
Student stu2 = (Bean(json, Student.class); System.out.String());
详细代码:
public static void main(String[] args) {
//定义对象
Student stu = new Student("张三", "123456");
//JavaBean对象转json字符串
JSONObject jsonObject = JSONObject.fromObject(stu);
System.out.println(jsonObject);
//json字符串转为javaBean
//定义json字符串
String jsondata = "{\"username\":\"李四\", \"password\":\"123\"}"; //转为json对象
JSONObject json = JSONObject.fromObject(jsondata);
//转为JavaBean对象
Student stu2 = (Bean(json, Student.class); System.out.String());
}
List与json字符串互转:
先定义list集合,list转json字符串
//定义list集合
List list = new ArrayList();
list.add(new Student("张三", "123"));
list.add(new Student("李四", "456"));
//list转json字符串
JSONArray jsonArray = JSONArray.fromObject(list);
System.out.println(jsonArray);
json字符串转list
//json字符串转list
List list2 = new ArrayList();
String jsondata = "[{\"password\":\"123\",\"username\":\"张三\"},{\"password\":\"456\",\"username\":\"李四\"}]";
JSONArray jsonArray1 = JSONArray.fromObject(jsondata);
for(int i = 0; i < jsonArray1.size(); i++) {
JSONObject jsonObject2 = JSONObject(i); //获取到每⼀条. 0:[{\"password\":\"123\",\"username\":\"张三\"} ,1:{\"password\":\"456\",\"username\":\"李四\"}
Student stu2 = (Bean(jsonObject2, Student.class);
list2.add(stu2);wozuile
}
System.out.println(list2);
全部代码:
public static void main(String[] args) {
//定义list集合
List list = new ArrayList();
list.add(new Student("张三", "123"));
list.add(new Student("李四", "456"));
//list转json字符串
JSONArray jsonArray = JSONArray.fromObject(list);
System.out.println(jsonArray);
//json字符串转list
List list2 = new ArrayList();
String jsondata = "[{\"password\":\"123\",\"username\":\"张三\"},{\"password\":\"456\",\"username\":\"李四\"}]";
JSONArray jsonArray1 = JSONArray.fromObject(jsondata);
for(int i = 0; i < jsonArray1.size(); i++) {
JSONObject jsonObject2 = JSONObject(i);
Student stu2 = (Bean(jsonObject2, Student.class);
list2.add(stu2);
}
System.out.println(list2);
}
Map与json字符串互转:
//定义map集合
Map map = new HashMap();
map.put("1", new Student("张三", "123"));
map.put("2", new Student("李四", "456"));
//Map转json字符串
JSONObject jsonMap = JSONObject.fromObject(map);
System.out.println(jsonMap);
定义字符串map集合,map集合字符串转为map:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论