fastJson使用
来源:互联网 发布:c语言中删除文件 编辑:程序博客网 时间:2024/05/20 06:40
对于服务端返回的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; }}
fastJson解析方法:
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<String, Object> 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()); } /** * * 功能描述: <br> * 〈功能详细描述〉 构造key:value 键值对 */ public static StringBuilder buildJsonParamByMapObject( Map<String, Object> params, String key) { StringBuilder builder = new StringBuilder(); String val = JSON.toJSONString(params); builder.append("\"" + key + "\":" + val); return builder; } /** * * 功能描述: <br> * 〈功能详细描述〉 构造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; }}
fastJson可以解析任何类型的基础数据类型例如int、 string、boolen
我写的一个小Demo http://download.csdn.net/detail/yaya_soft/8256245
0 0
- 使用fastJSON
- FastJson使用
- FastJSON使用
- fastJson使用
- fastjson 使用
- fastjson 使用
- FastJson使用
- fastjson使用
- fastJSON使用
- FastJson 使用
- FastJson使用
- FastJson使用
- fastjson使用
- FastJson使用
- Fastjson使用
- FastJson 使用
- fastjson使用
- fastjson使用
- iOS开发--CocoaPods pod install/pod update更新慢的问题
- Fragment放置后台很久(Home键退出很长时间),返回时出现Fragment重叠解决方案
- ARM信号量操作指令
- ListActivity
- swift学习---长句部分五(第一次计划看一本完整的英文资料,你是最棒的,加油)
- fastJson使用
- hdu3652:B-number
- 黑马程序员:论反射的正确使用以及应用范围2·个人感悟 反射有啥用?
- Longest Common Prefix
- 网站(Web)压测工具Webbench源码分析
- 【Java.NIO】NIO的长连接及NIO的内存泄漏
- 去除自定义alertdialog(dialog)黑边
- 第十六周OJ之指数引出奇数因子
- 《C++程序设计原理与实践》部分习题答案 1