对于服务端返回的json格式数据,解析的方案还是不比较多的,这里推荐 fastjson,原因是使用简单、效率还是比较高的
fastjson主要是我们在解析的时候要有对应的本地Bean来承接返回的jsonObject和jsonArray,直接通过一个例子来看看:
假设我们从服务端获取的数据为:
String response = "{\"id\":\"2\",\"name\":\"节点01\",\"open\":true,\"pId\":\"1\",\"tree\":{\"color\":\"gree\",\"tree\":true}}";
这是一个标准的json格式串,通过分析我们发现其结构分为两个部分:
一个外围的Bean,有基本属性id、name、open、tree,以及一个Bean类型的属性tree.这个内部的bean也有自己的属性color、tree
那我们就本地生成对应的bean
外部Bean:
package lavasoft.stu.json;
public class Node {
private String id;
private String pId;
private String name;
private boolean open;
private Tree tree;
public Node(String id, String pId, String name, boolean open, Tree tree) {
super();
this.id = id;
this.pId = pId;
this.name = name;
this.open = open;
this.tree = tree;
}
public Node() {
// TODO Auto-generated constructor stub
}
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;
}
public Tree getTree() {
return tree;
}
public void setTree(Tree tree) {
this.tree = tree;
}
}
内部Bean:
package lavasoft.stu.json;
public class Tree {
String color;
boolean isTree;
public Tree(String color, boolean isTree) {
super();
this.color = color;
this.isTree = isTree;
}
public Tree() {
// TODO Auto-generated constructor stub
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public boolean isTree() {
return isTree;
}
public void setTree(boolean isTree) {
this.isTree = isTree;
}
}
package lavasoft.stu.json;
import java.util.HashMap;
import java.util.Map;
import com.alibaba.fastjson.JSON;
public class MainTest {
public static void main(String[] args) {
// 对象构造
Node node = new Node("1", "12", "jone", false, new Tree("green", true));
String jsonStr = JSON.toJSONString(node);
System.out.println(jsonStr);
Node node3 = JSON.parseObject(jsonStr, Node.class);
System.out.println("FastJson解析结果[对象]" + node3.getName());
System.out.println("////////////////////////");
// 字符串构造
String response = "{\"id\":\"2\",\"name\":\"节点01\",\"open\":true,\"pId\":\"1\",\"tree\":{\"color\":\"gree\",\"tree\":true}}";
Node node4 = JSON.parseObject(response, Node.class);
System.out.println("FastJson解析结果【字符串】:" + " is tree: "
+ node4.getTree().isTree() + " **color: "
+ node4.getTree().getColor() + "**name: " + node4.getName()
+ "**isopen: " + node4.isOpen());
// util方法构造
System.out.println("////////////////////////");
Map map = new HashMap<>();
map.put("color", "blue");
map.put("tree", true);
StringBuilder builder = new StringBuilder();
builder.append("{").append(buildJsonParamByStrObject("节点02", "name"))
.append(",").append(buildJsonParamByStrObject(true, "open"))
.append(",").append(buildJsonParamByMapObject(map, "tree"))
.append("}");
System.out.println("builder " + builder.toString());
Node node2 = JSON.parseObject(builder.toString(), Node.class);
System.out.println("FastJson解析结果【util】:" + " is tree: "
+ node2.getTree().isTree() + " **color: "
+ node2.getTree().getColor() + "**name: " + node2.getName()
+ "**isopen: " + node2.isOpen());
}
/**
*
* 功能描述:
* 〈功能详细描述〉 构造key:value 键值对
*/
public static StringBuilder buildJsonParamByMapObject(
Map params, String key) {
StringBuilder builder = new StringBuilder();
String val = JSON.toJSONString(params);
builder.append("\"" + key + "\":" + val);
return builder;
}
/**
*
* 功能描述:
* 〈功能详细描述〉 构造key:value 键值对
*/
public static StringBuilder buildJsonParamByStrObject(Object params,
String key) {
StringBuilder builder = new StringBuilder();
String val = JSON.toJSONString(params);
builder.append("\"" + key + "\":" + val);
return builder;
}
}