关于JSON对象的解析,以及阿里fastjson中一个API的使用
来源:互联网 发布:免费空间支持域名绑定 编辑:程序博客网 时间:2024/06/05 20:48
Fastjson介绍
Fastjson是一个Java语言编写的JSON处理器,由阿里巴巴公司开发。
1、遵循http://json.org标准,为其官方网站收录的参考实现之一。
2、功能qiang打,支持JDK的各种类型,包括基本的JavaBean、Collection、Map、Date、Enum、泛型。
3、无依赖,不需要例外额外的jar,能够直接跑在JDK上。
4、开源,使用Apache License 2.0协议开源。http://code.alibabatech.com/wiki/display/FastJSON/Home
5、具有超高的性能,java世界里没有其他的json库能够和fastjson可相比了。
如果获得Fastjson?
SVN:http://code.alibabatech.com/svn/fastjson/trunk/
WIKI:http://code.alibabatech.com/wiki/display/FastJSON/Home
Issue Tracking:http://code.alibabatech.com/jira/browse/FASTJSON
使用介绍:
Fastjson的最主要的使用入口是com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSON;public static final Object parse(String text); // 把JSON文本parse为JSONObject或者JSONArraypublic static final JSONObject parseObject(String text); // 把JSON文本parse成JSONObjectpublic static final <T> T parseObject(String text, Class<T> clazz); // 把JSON文本parse为JavaBeanpublic static final JSONArray parseArray(String text); // 把JSON文本parse成JSONArraypublic static final <T> List<T> parseArray(String text, Class<T> clazz); //把JSON文本parse成JavaBean集合public static final String toJSONString(Object object); // 将JavaBean序列化为JSON文本public static final String toJSONString(Object object, boolean prettyFormat); // 将JavaBean序列化为带格式的JSON文本public static final Object toJSON(Object javaObject); 将JavaBean转换为JSONObject或者JSONArray。
今天突然原先项目有个需求,需要将微信接口返回回来的JSON字符串解析出来获得需要的数据,因此就顺着需求做了个小小的Demo,顺便吐槽下,fastjson
所有接口的参数都需要传入一个String类型的JSON,不支持解析文件啊,读取文件也是个麻烦的事,所以就把刚刚读取文件的代码一并发了
public String readFile(){ File file = new File("C:\\JAVA\\test.txt"); BufferedReader reader = null; String laststr = ""; try { //System.out.println("以行为单位读取文件内容,一次读一整行:"); reader = new BufferedReader(new FileReader(file)); String tempString = null; //一次读入一行,直到读入null为文件结束 while ((tempString = reader.readLine()) != null) { laststr = laststr+tempString; } // laststr=laststr.substring(2); // System.out.println("1"+laststr); reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } return laststr; }整个代码段如下
import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;public class JackJsonDemo { public static void main(String[] args) { JackJsonDemo jjd = new JackJsonDemo(); //解析readFile这个方法读出来的String类型的JSON转换成JSON对象 JSONObject json = JSON.parseObject(jjd.readFile()); //解析JSON字符串,获取Object类型的对象 Object obj = json.get("user_info"); String str1 =obj.toString(); //因为好多JSON字符串解析出来都是树结构,因此可以再次解析这个节点成为一个JSON对象 JSONObject json1 =JSON.parseObject(str1); Object obj1=json1.get("fake_id"); System.out.println(obj1); }public String readFile(){ File file = new File("C:\\JAVA\\test.txt"); BufferedReader reader = null; String laststr = ""; try { //System.out.println("以行为单位读取文件内容,一次读一整行:"); reader = new BufferedReader(new FileReader(file)); String tempString = null; //一次读入一行,直到读入null为文件结束 while ((tempString = reader.readLine()) != null) { laststr = laststr+tempString; } // laststr=laststr.substring(2); // System.out.println("1"+laststr); reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } return laststr; }}
今天就用了个这个其他的没有用了!有时间在研究!
0 0
- 关于JSON对象的解析,以及阿里fastjson中一个API的使用
- 来自阿里的 json 解析方案 fastjson
- Alibaba FastJson支持对象中私有属性的json解析
- java中解析一个json串,本文详细介绍了安卓原生的解析以及用fastjson解析
- 阿里FastJson的使用
- JSON的解析 --之 FastJson 的使用
- Json解析包 阿里巴巴 FastJson 的使用
- Java的Json解析包FastJson使用
- fastJson API 最快的Json和对象转换工具
- 阿里fastjson 对象转JSON
- 阿里fastjson的使用实例
- 阿里的fastjson使用总结
- 关于数据序列化(3),使用JSON的方式,以及几个注意的点,以及fastJson的坑
- JSON的解析[Java-JSON、FastJSON、GSON]
- 使用fastjson解析较为复杂的json数据。
- Android常用Json解析框架GSON和FastJson的使用
- SpringBoot 之 第三方Json解析框架FastJson的使用
- 使用Gson和FastJson解析JSON数据遇到的问题
- linux grep命令
- IOS获取网络类型方法分析
- hibernate优化
- 电压互感器的分类和基本术语
- DBlink创建 查询
- 关于JSON对象的解析,以及阿里fastjson中一个API的使用
- Bit Map 位图结构
- real210移植记录-支持eMMC,增加菜单操作
- 那些年追过的明星
- Qt下Client与Server之间的双向TCP通信。
- service(1)
- poj1611 The Suspects(并查集)
- c语言 可变数组
- osg模型重写