• 使用fastJson 来解析 json


    FastJson 是阿里巴巴出的,据说是速度最快的。

    java类

     1 package com.android.domain;
     2 
     3 public class Person {
     4 
     5     private String name;
     6     private int age;
     7     private String address;
     8 
     9     public String getName() {
    10         return name;
    11     }
    12 
    13     public Person(String name, int age, String address) {
    14         super();
    15         this.name = name;
    16         this.age = age;
    17         this.address = address;
    18     }
    19 
    20     @Override
    21     public String toString() {
    22         return "Person [name=" + name + ", age=" + age + ", address=" + address
    23                 + "]";
    24     }
    25 
    26     public void setName(String name) {
    27         this.name = name;
    28     }
    29 
    30     public int getAge() {
    31         return age;
    32     }
    33 
    34     public void setAge(int age) {
    35         this.age = age;
    36     }
    37 
    38     public String getAddress() {
    39         return address;
    40     }
    41 
    42     public void setAddress(String address) {
    43         this.address = address;
    44     }
    45 
    46     public Person() {
    47         // TODO Auto-generated constructor stub
    48     }
    49 
    50 }

    执行类:

     1 package com.android.myfastjson;
     2 
     3 import java.util.ArrayList;
     4 import java.util.HashMap;
     5 import java.util.List;
     6 import java.util.Map;
     7 
     8 import com.alibaba.fastjson.JSON;
     9 import com.alibaba.fastjson.TypeReference;
    10 import com.alibaba.fastjson.asm.Type;
    11 import com.alibaba.fastjson.util.TypeUtils;
    12 
    13 public class FastJsonTools {
    14 
    15     public FastJsonTools() {
    16         // TODO Auto-generated constructor stub
    17     }
    18 
    19     public static String createJsonString(Object object) {
    20         String jsonString = JSON.toJSONString(object);
    21         return jsonString;
    22     }
    23 
    24     public static <T> T createJsonBean(String jsonString, Class<T> cls) {
    25         T t = JSON.parseObject(jsonString, cls);
    26         return t;
    27     }
    28 
    29     public static <T> List<T> createJsonToListBean(String jsonString,
    30             Class<T> cls) {
    31         List<T> list = null;
    32         list = JSON.parseArray(jsonString, cls);
    33         return list;
    34     }
    35 
    36     /**
    37      * @param jsonString
    38      * @return
    39      */
    40     public static List<Map<String, Object>> createJsonToListMap(
    41             String jsonString) {
    42         List<Map<String, Object>> list2 = JSON.parseObject(jsonString,
    43                 new TypeReference<List<Map<String, Object>>>() {
    44                 });
    45         return list2;
    46     }
    47 
    48     /**
    49      * @param args
    50      */
    51     public static void main(String[] args) {
    52         // TODO Auto-generated method stub
    53         // Person person1 = new Person("张三1", 24, "北京1");
    54         // Person person2 = new Person("张三2", 23, "北京2");
    55         // List<Person> list = new ArrayList<Person>();
    56         // list.add(person1);
    57         // list.add(person2);
    58         List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    59         Map<String, Object> map = new HashMap<String, Object>();
    60         map.put("name", "jack");
    61         map.put("age", 23);
    62         Map<String, Object> map2 = new HashMap<String, Object>();
    63         map2.put("name", "rose");
    64         map2.put("age", 24);
    65         list.add(map);
    66         list.add(map2);
    67 
    68         String jsonString = JSON.toJSONString(list);
    69         System.out.println(jsonString);
    70         // JSON.parseArray(arg0, arg1)
    71         List<Map<String, Object>> list2 = JSON.parseObject(jsonString,
    72                 new TypeReference<List<Map<String, Object>>>() {
    73                 });
    74         // List<Person> lists = JSON.parseArray(arg0, arg1);
    75         System.out.println(list2.toString());
    76 
    77     }
    78 
    79 }

    输出:

    [{"age":23,"name":"jack"},{"age":24,"name":"rose"}]

    [{name=jack, age=23}, {name=rose, age=24}]

  • 相关阅读:
    asp.net控件开发基础(转)
    如何在C#中直接操作C++结构体(转)
    如何打造自己的代码段
    WeifenLuo.WinFormsUI.Docking.dll 源码分析(一)
    软件竞标流程与要点【转】
    C#进制转换
    使用 DpaToolkit 对 C#类库进行反向建模
    算法的时间复杂度(计算实例)
    C#操作SQLServer的Image字段
    不同进制之间的转换
  • 原文地址:https://www.cnblogs.com/aosting/p/3628988.html
Copyright © 2020-2023  润新知