String、JSONObject以及javaBean之间的转换

1.导入的jar包
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.47</version>
</dependency>

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.16.18</version>
    <scope>compile</scope>
</dependency>
2.实体类
package com.gongsl.notebook.copy.pojo;

import lombok.Data;

/**
 * @Author: gongsl
 * @Date: 2020-04-27 16:28
 */
@Data
public class Person{

    //编号
    private int id;

    //姓名
    private String name;

    //年龄
    private String age;

    //性别
    private String gender;
}
3.转换的逻辑
3.1 String—>javaBean
package com.gongsl.test;

import com.alibaba.fastjson.JSONObject;
import com.gongsl.notebook.copy.pojo.Person;

/**
 * @Author: gongsl
 * @Date: 2020-11-25 23:48
 */
public class JsonTest {
    public static void main(String[] args) {
        String str = "{\"id\":1,\"name\":\"张三\",\"age\":\"25\",\"gender\":\"男\"}";
        
        //String--->javaBean
        Person person = JSONObject.parseObject(str, Person.class);
        System.out.println(person);
    }
}

//运行结果---> Person(id=1, name=张三, age=25, gender=男)
3.2 String—>JSONObject
package com.gongsl.test;

import com.alibaba.fastjson.JSONObject;

/**
 * @Author: gongsl
 * @Date: 2020-11-25 23:48
 */
public class JsonTest {
    public static void main(String[] args) {
        String str = "{\"id\":1,\"name\":\"张三\",\"age\":\"25\",\"gender\":\"男\"}";
        
        //String--->JSONObject
        JSONObject jsonObject = JSONObject.parseObject(str);
        System.out.println(jsonObject);
    }
}

//运行结果---> {"id":1,"age":"25","name":"张三","gender":"男"}
3.3 javaBean—>String
package com.gongsl.test;

import com.alibaba.fastjson.JSONObject;
import com.gongsl.notebook.copy.pojo.Person;

/**
 * @Author: gongsl
 * @Date: 2020-11-25 23:48
 */
public class JsonTest {
    public static void main(String[] args) {
        Person person = new Person();
        person.setId(1);
        person.setName("张三");
        person.setAge("25");
        person.setGender("男");
        
        //javaBean--->String
        String str = JSONObject.toJSONString(person);
        System.out.println(str);
    }
}

//运行结果---> {"age":"25","gender":"男","id":1,"name":"张三"}
3.4 JSONObject—>String
package com.gongsl.test;

import com.alibaba.fastjson.JSONObject;

/**
 * @Author: gongsl
 * @Date: 2020-11-25 23:48
 */
public class JsonTest {
    public static void main(String[] args) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id",1);
        jsonObject.put("name","张三");
        jsonObject.put("age","25");
        jsonObject.put("gender","男");
        
        //JSONObject--->String
        String str = jsonObject.toString();
        System.out.println(str);
    }
}

//运行结果---> {"id":1,"age":"25","name":"张三","gender":"男"}
3.5 javaBean—>JSONObject
package com.gongsl.test;

import com.alibaba.fastjson.JSONObject;
import com.gongsl.notebook.copy.pojo.Person;

/**
 * @Author: gongsl
 * @Date: 2020-11-25 23:48
 */
public class JsonTest {
    public static void main(String[] args) {
        Person person = new Person();
        person.setId(1);
        person.setName("张三");
        person.setAge("25");
        person.setGender("男");
        
        //javaBean--->JSONObject
        JSONObject jsonObject = (JSONObject) JSONObject.toJSON(person);
        System.out.println(jsonObject);
    }
}

//运行结果---> {"id":1,"name":"张三","age":"25","gender":"男"}
3.6 JSONObject—>javaBean
package com.gongsl.test;

import com.alibaba.fastjson.JSONObject;
import com.gongsl.notebook.copy.pojo.Person;

/**
 * @Author: gongsl
 * @Date: 2020-11-25 23:48
 */
public class JsonTest {
    public static void main(String[] args) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id",1);
        jsonObject.put("name","张三");
        jsonObject.put("age","25");
        jsonObject.put("gender","男");

        //JSONObject--->javaBean
        Person person = JSONObject.toJavaObject(jsonObject, Person.class);
        System.out.println(person);
    }
}

//运行结果---> Person(id=1, name=张三, age=25, gender=男)
JSONObject的fromObject方法是net.sf.json.JSONObject类中的一个静态方法,用于将Java对象转换为JSON对象。它接受一个参数,该参数可以是Map、List、Array、JavaBean等常见的Java数据类型。fromObject方法会将传入的Java对象转换为对应的JSON对象,并返回该JSON对象。 fromObject方法的使用示例有如下几种: 1. 将List集合转换为JSON对象: ```java List<String> list = new ArrayList<>();list.add("first"); list.add("second"); JSONArray jsonArray = JSONArray.fromObject(list); ``` 此代码将一个包含两个字符串元素的List集合转换为JSON数组对象。 2. 将Map集合转换为JSON对象: ```java Map<String, Object> map = new HashMap<>(); map.put("name", "json"); map.put("bool", Boolean.TRUE); map.put("int", new Integer(1)); map.put("arr", new String[] { "a", "b" }); map.put("func", "function(i){ return this.arr[i]; }"); JSONObject json = JSONObject.fromObject(map); ``` 此代码将一个包含不同类型键值对的Map集合转换为JSON对象。 3. 将JavaBean转换为JSON对象: ```java JSONObject jsonObject = JSONObject.fromObject(new JsonBean()); ``` 此代码将一个自定义的JavaBean对象转换为JSON对象。 4. 将数组转换为JSON对象: ```java boolean[] boolArray = new boolean[] { true, false, true }; JSONArray jsonArray = JSONArray.fromObject(boolArray); ``` 此代码将一个boolean类型的数组转换为JSON数组对象。 5. 将一般数据转换为JSON对象: ```java JSONArray jsonArray = JSONArray.fromObject("['json','is','easy']"); ``` 此代码将一个包含三个字符串元素的一般数据转换为JSON数组对象。 总结起来,JSONObject的fromObject方法可以将Java对象转换为对应的JSON对象,无论是List、Map、JavaBean还是数组或一般数据都可以进行转换。该方法的使用非常简单,只需传入相应的Java对象即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值