关于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
原创粉丝点击