使用FastJson对数据的常用操作

          Fastjson是阿里巴巴提供的一个Java语言编写的高性能功能完善的JSON库,可用于将Java对象转换为JSON表示形式。它也可以用于将JSON字符串转换为等效的Java对象。Fastjson可以使用任意Java对象,包括您没有源代码的预先存在的对象。

        开源地址:https://github.com/alibaba/fastjson


      Maven配置

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.24</version>
        </dependency>



代码示例

package example;

/**
 * @description:使用Fastjson
 * @author:JBL
 * @date:2017/4/10
 */

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import model.User;

import java.util.*;

/**
 * Fastjson,是阿里巴巴提供的一个Java语言编写的高性能功能完善的JSON库。
 * 其开源的下载网址为:https://github.com/alibaba/fastjson。
 */

public class FastJsonExample {

    /**
     * String 转 jsonObject
     */
    public static  void  getStrToJSONObject(){
        StringBuffer buffer=new StringBuffer();
        buffer.append("{");
        buffer.append("\"age\":").append("27").append(",");
        buffer.append("\"name\":").append("\"张三\"").append(",");
        buffer.append("\"password\":").append("\"12567\"");
        buffer.append("}");

        String jsonText=buffer.toString();
        JSONObject jobj=JSON.parseObject(jsonText);
        System.out.println(jobj.get("password"));
    }

    /**
     * String 转 User对象
     */
    public static  User getStrToObject(){
        StringBuffer buffer=new StringBuffer();
        buffer.append("{");
        buffer.append("\"age\":").append("27").append(",");
        buffer.append("\"name\":").append("\"张三\"").append(",");
        buffer.append("\"password\":").append("\"12567\"");
        buffer.append("}");

        String jsonText=buffer.toString();
        return JSON.parseObject(jsonText,User.class);
    }


    /**
     * 通过fastjson把字符串转换成泛型数组
     * TypeReference
     */
    public static void string2List(){
        StringBuffer buffer=new StringBuffer();
        buffer.append("[{");
        buffer.append("\"age\":").append("27").append(",");
        buffer.append("\"name\":").append("\"张三\"").append(",");
        buffer.append("\"password\":").append("\"12567\"");
        buffer.append("}]");

        String jsonText=buffer.toString();
        //转成成数组
        User[] stu2 = JSON.parseObject(jsonText,new TypeReference<User[]>(){});
        List<User> list = Arrays.asList(stu2);

        for(User st:list){
            System.out.println(st.getName());
        }

        // 转换成ArrayList
        ArrayList<User> list2 = JSON.parseObject(jsonText, new TypeReference<ArrayList<User>>(){});

        for (int i = 0; i < list2.size(); i++) {
            User obj =(User) list2.get(i);
            System.out.println(obj.getName());
        }
    }

    /**
     * 通过fastjson字符串转成List<User>对象
     */

    public static String getListObject(){
        List<User> userList = new ArrayList<>();
        User   user1 = new User();
               user1.setName("haha");
               user1.setPassword("11111111");
        userList.add(user1);
        User   user2 = new User();
               user2.setName("haha2");
               user2.setPassword("22222");
        userList.add(user2);
        //List 对象转JSON 字符串
       String userStr=  JSON.toJSONString(userList);
         //JSON 字符串 转 List 对象
        List<User> userList2 = JSON.parseArray(userStr,User.class);
        System.out.println(userList2.get(0).getName());
    return userStr;
    }


    /**
     * 通过fastjson把Map换成字符串转
     */
    public static void map2json(){
        //创建一个Map对象
        Map<String,String> map = new HashMap<String, String>();
        map.put("username", "张三");
        map.put("password", "2222");
        map.put("age", "198");
        String json = JSON.toJSONString(map,true); //转成JSON数据

        Map<String,String> map1 = (Map<String,String>)JSON.parse(json);
        //遍历数组数据
        for (String key : map1.keySet()) {
            System.out.println(key+":"+map1.get(key));
        }
    }

    /**
     * 通过fastjson把Map换成字符串转
     */
    public static void map2JSON() {
        Map map = new HashMap();
        map.put("name", "张三");
        map.put("password", "3333");
        map.put("age", "198");
        String json = JSON.toJSONString(map);
        Map map1 = JSON.parseObject(json);
        for (Object obj : map.entrySet()) {
            Map.Entry<String, String> entry = (Map.Entry<String, String>) obj;
            System.out.println(entry.getKey() + "--->" + entry.getValue());
        }
    }

    public static void main(String[] args) {
        //System.out.println(getStrToObject().getPassword());
       // System.out.println(string2List());
        //System.out.println(map2json());
        System.out.println(getListObject());


    }
}

                                                                                      想了解更多加微信公众号(jblPaul)

                                                                                                  





  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值