字符串 Java对象 互相转换 您所在的位置:网站首页 java怎么把字符串转换为字符数组格式 字符串 Java对象 互相转换

字符串 Java对象 互相转换

2024-07-17 12:33| 来源: 网络整理| 查看: 265

基于Json-Lib、Org.Json、Jackson、Gson、FastJson五种方式转换json类型

json-lib时间有些久远,jar包只更新到2010年

org.json用起来有些繁琐

Jackson、Gson、FastJson只需一两句话就可以搞定

1、FastJson

Maven依赖:

com.alibaba fastjson 1.2.37

测试demo

import com.alibaba.fastjson.JSON; public class FastJsonDemo { public static void main(String[] args) { //创建测试object User user = new User("李宁",24,"北京"); System.out.println(user); //转成json字符串 String json = JSON.toJSON(user).toString(); System.out.println(json); //json字符串转成对象 User user1 = JSON.parseObject(json,User.class); System.out.println(user1); } }

2、Json-Lib

json-lib时间有些久远,jar包只更新到2010年

Maven依赖:

net.sf.json-lib json-lib 2.2.3 jdk15 public static void jsonStrToJava(){ //定义两种不同格式的字符串 String objectStr="{\"name\":\"JSON\",\"age\":\"24\",\"address\":\"北京市海淀区\"}"; String arrayStr="[{\"name\":\"JSON\",\"age\":\"24\",\"address\":\"北京市海淀区\"}]"; //1、使用JSONObject JSONObject jsonObject=JSONObject.fromObject(objectStr); Student stu=(Student)JSONObject.toBean(jsonObject, Student.class); //2、使用JSONArray JSONArray jsonArray=JSONArray.fromObject(arrayStr); //获得jsonArray的第一个元素 Object o=jsonArray.get(0); JSONObject jsonObject2=JSONObject.fromObject(o); Student stu2=(Student)JSONObject.toBean(jsonObject2, Student.class); System.out.println("stu:"+stu); System.out.println("stu2:"+stu2); }

打印结果为:

stu:Student [name=JSON, age=24, address=北京市海淀区] stu2:Student [name=JSON, age=24, address=北京市海淀区]

3、org.json

Maven依赖:

org.json json 20170516

测试demo

import org.json.JSONObject; public class OrgJsonDemo { public static void main(String[] args) { //创建测试object User user = new User("李宁",24,"北京"); System.out.println(user); //转成json字符串 String json = new JSONObject(user).toString(); System.out.println(json); //json字符串转成对象 JSONObject jsonObject = new JSONObject(json); String name = jsonObject.getString("name"); Integer age = jsonObject.getInt("age"); String location = jsonObject.getString("location"); User user1 = new User(name,age,location); System.out.println(user1); } }

4、Jackson

Maven依赖:

com.fasterxml.jackson.core jackson-databind 2.9.0

测试demo

import com.fasterxml.jackson.databind.ObjectMapper; public class JacksonDemo { public static void main(String[] args) { //创建测试object User user = new User("李宁",24,"北京"); System.out.println(user); //转成json字符串 ObjectMapper mapper = new ObjectMapper(); try { String json = mapper.writeValueAsString(user); System.out.println(json); //json字符串转成对象 User user1 = mapper.readValue(json,User.class); System.out.println(user1); } catch (java.io.IOException e) { e.printStackTrace(); } } }

5、Gson

Maven依赖:

com.google.code.gson gson 2.8.1

测试demo

import com.google.gson.Gson; public class GsonDemo { public static void main(String[] args) { //创建测试object User user = new User("李宁",24,"北京"); System.out.println(user); //转成json字符串 Gson gson = new Gson(); String json = gson.toJson(user); System.out.println(json); //json字符串转成对象 User user1 = gson.fromJson(json,User.class); System.out.println(user1); } }

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

      专题文章
        CopyRight 2018-2019 实验室设备网 版权所有