fastjson使用实例

    Fastjson是一个阿里开源出来的一个Java语言编写的高性能功JSON库,基本使用介绍如下:

POM:

<dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>fastjson</artifactId>
     <version> 1.1 . 41 </version>
</dependency>

  以崩溃后台的查询为例:

1、崩溃后台查询的返回如下:

{"statistic_data":{"total_num":"xxx","crash_hash_num":"xxx","mid_num":"xxx"},"table_data":[{"crash_hash":"xxxxxxxxxxxxxxxxx","version":"x.x.x.xxx","first_time":"1433501742","last_time":"1433865273","assigned_module":"xxx.xxxx.xxxx","assigned_group":"xxx","num":"xxx","mid_num":"xxxx"},{……},……],"pageNum":"0"}

2、从返回内容可以看出,是一个比较标准的json串,所以根据json串格式,定义如下几个类:

CrashQueryResult.java

import  java.util.ArrayList;
import  java.util.List;
public  class  CrashQueryResult {
     private  CrashQueryInfo statistic_data;
     private  ArrayList<CrashHashElement> table_data =  new  ArrayList<CrashHashElement>();
     private  String pageNum;
     public  CrashQueryInfo getStatistic_data() {
         return  statistic_data;
     }
     public  void  setStatistic_data(CrashQueryInfo statistic_data) {
         this .statistic_data = statistic_data;
     }
     public  ArrayList<CrashHashElement> getTable_data() {
         return  table_data;
     }
     public  void  setTable_data(ArrayList<CrashHashElement> table_data) {
         this .table_data = table_data;
     }
     public  String getPageNum() {
         return  pageNum;
     }
     public  void  setPageNum(String pageNum) {
         this .pageNum = pageNum;
     }
     @Override
     public  String toString() {
         return  "CrashQueryResult [statistic_data="  + statistic_data
                 ", table_data="  + table_data +  ", pageNum="  + pageNum +  "]" ;
     }
     
}
CrashQueryInfo.java

import  java.util.ArrayList;
 
public  class  CrashQueryInfo {
     private  String total_num;
     private  String crash_hash_num;
     private  String mid_num;
     public  String getTotal_num() {
         return  total_num;
     }
     public  void  setTotal_num(String total_num) {
         this .total_num = total_num;
     }
     public  String getCrash_hash_num() {
         return  crash_hash_num;
     }
     public  void  setCrash_hash_num(String crash_hash_num) {
         this .crash_hash_num = crash_hash_num;
     }
     public  String getMid_num() {
         return  mid_num;
     }
     public  void  setMid_num(String mid_num) {
         this .mid_num = mid_num;
     }
     @Override
     public  String toString() {
         return  "CrashQueryInfo [total_num="  + total_num +  ", crash_hash_num="
                 + crash_hash_num +  ", mid_num="  + mid_num +  "]" ;
     }
     
}
CrashHashElement.java
public  class  CrashHashElement {
//  查询crash hash
     private  String crash_hash;
//  查询版本
     private  String version;
//  查询开始时间
     private  String first_time;
//  查询截止时间
     private  String last_time;
//  崩溃堆栈模块
     private  String assigned_module;
//  崩溃所属模块
     private  String assigned_group;
//  崩溃次数
     private  String num;
//  用户数
     private  String mid_num;
     public  String getCrash_hash() {
         return  crash_hash;
     }
     public  void  setCrash_hash(String crash_hash) {
         this .crash_hash = crash_hash;
     }
     public  String getVersion() {
         return  version;
     }
     public  void  setVersion(String version) {
         this .version = version;
     }
     public  String getFirst_time() {
         return  first_time;
     }
     public  void  setFirst_time(String first_time) {
         this .first_time = first_time;
     }
     public  String getLast_time() {
         return  last_time;
     }
     public  void  setLast_time(String last_time) {
         this .last_time = last_time;
     }
     public  String getAssigned_module() {
         return  assigned_module;
     }
     public  void  setAssigned_module(String assigned_module) {
         this .assigned_module = assigned_module;
     }
     public  String getAssigned_group() {
         return  assigned_group;
     }
     public  void  setAssigned_group(String assigned_group) {
         this .assigned_group = assigned_group;
     }
     public  String getNum() {
         return  num;
     }
     public  void  setNum(String num) {
         this .num = num;
     }
     public  String getMid_num() {
         return  mid_num;
     }
     public  void  setMid_num(String mid_num) {
         this .mid_num = mid_num;
     }
     @Override
     public  String toString() {
         return  "CrashHashElement [crash_hash="  + crash_hash +  ", version="
                 + version +  ", first_time="  + first_time +  ", last_time="
                 + last_time +  ", assigned_module="  + assigned_module
                 ", assigned_group="  + assigned_group +  ", num="  + num
                 ", mid_num="  + mid_num +  "]" ;
     }
     
     
}

 

3、将字符串信息转化为我们定义的json对象

public  void  parseStringToObj(String strInput)
{
     CrashQueryResult crashQueryResult = JSON.parseObject(strInput, CrashQueryResult. class );
}

注意:   

主要成员变量的命名方式需要和json串中的命名一致,并且以此来生成对应的set和get方法


JSON数据格式简洁,用于数据的持久化和对象传输很实用。 java下有个大名鼎鼎的阿里巴巴开源的Java的JSON处理器 fastjson,.net也有个.net版的fastjson。这里是作者做的性能测试:代码调用namespace test {     class Program     {         static void Main(string[] args)         {             var zoo1 = new zoo();             zoo1.animals = new List<animal>();             zoo1.animals.Add(new cat() { Name = "hello kitty", legs = 4 });             zoo1.animals.Add(new dog() { Name = "dog1", tail = true });             string json= fastJSON.JSON.Instance.ToJSON(zoo1); //序列化             var z = fastJSON.JSON.Instance.ToObject<zoo>(json); //反序列化             Console.WriteLine(z.animals[0].Name);             Console.Read();         }     }     public class animal { public string Name { get; set; } }     public class cat : animal { public int legs { get; set; } }     public class dog : animal { public bool tail { get; set; } }     public class zoo { public List<animal> animals { get; set; }  }基本的调用就是这么简单! 需要注意的是要反序列化的类好像必须声明为public的。快速的秘密 大体浏览了一下代码,发现之所以快速的原因是作者利用反射时Emit了大量的IL代码:internal object FastCreateInstance(Type objtype)         {             try             {                 CreateObject c = null;                 if (_constrcache.TryGetValue(objtype, out c))                 {                     return c();                 }                 else                 {                     if (objtype.IsClass)                      {                         DynamicMethod dynMethod = new DynamicMethod("_", objtype, null);                         ILGenerator ilGen = dynMethod.GetILGenerator();                         ilGen.Emit(OpCodes.Newobj, objtype.GetConstructor(Type.EmptyTypes));                         ilGen.Emit(OpCodes.Ret);                         c = (CreateObject)dynMethod.CreateDelegate(typeof(CreateObject));                         _constrcache.Add(objtype, c);                     }                     else // structs                     {                              DynamicMethod dynMethod = new DynamicMethod("_",                             MethodAttributes.Public | MethodAttributes.Static,                             CallingConventions.Standard,                             typeof(object),                             null,                             objtype, false);                         ILGenerator ilGen = dynMethod.GetILGenerator();                         var lv = ilGen.DeclareLocal(objtype);                         ilGen.Emit(OpCodes.Ldloca_S, lv);                         ilGen.Emit(OpCodes.Initobj, objtype);                         ilGen.Emit(OpCodes.Ldloc_0);                         ilGen.Emit(OpCodes.Box, objtype);                         ilGen.Emit(OpCodes.Ret);                         c = (CreateObject)dynMethod.CreateDelegate(typeof(CreateObject));                         _constrcache.Add(objtype, c);                     }                     return c();                 }             }             catch (Exception exc)             {                 throw new Exception(string.Format("Failed to fast create instance for type '{0}' from assemebly '{1}'",                     objtype.FullName, objtype.AssemblyQualifiedName), exc);             }         }更多教程请参考:http://www.codeproject.com/Articles/159450/fastJSON 标签:fastjson  json
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值