Maven工程下:alibaba fastjson2的各种序列化:java对象转json对象、json对象转java对象、json对象转字符串......

pom文件导入fastjson2坐标:

 <dependency>
      <groupId>com.alibaba.fastjson2</groupId>
      <artifactId>fastjson2</artifactId>
      <version>2.0.51</version>
    </dependency>

UserVO对象:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserVO {
    private Long id;
    private String userName;
    private String name;
    private String token;
}

java对象转json对象:

 //对象
         UserVO userVO = new UserVO(1L,"UserVo","User","0");
         //userVo对象:UserVO(id=1, userName=UserVo, name=User, token=0)
        System.out.println(userVO);
        //java对象转Json
        String jsonUserVo = JSONObject.toJSONString(userVO);
        //{"id":1,"name":"User","token":"0","userName":"UserVo"}
        System.out.println(jsonUserVo);

json对象(json字符串)转java对象:

UserVO userVO1 = JSON.parseObject(JSONObject.toJSONString(userVO),UserVO.class);
        //或者
        //UserVO userVO1 = JSON.parseObject(jsonUserVo,UserVO.class);
        //UserVO(id=1, userName=UserVo, name=User, token=0)
        System.out.println(userVO1);

其他:

 //java对象转换成json格式的字节码
        byte[]bytes=JSON.toJSONBytes(userVO);
        //读取字节码
        String s = new String(bytes);
        //result:{"id":1,"name":"User","token":"0","userName":"UserVo"}
        System.out.println(s);

json文件方式+JSONPath:

   // json文件:
"MyCar":["AstonMartin","Audio","AUDIOYYDS","AUDIOYYDS2","BENZ","BENZE","BENZYYDS","BMW",
    "BMWYYDS","BYD"],



public void testByteJson(){
        try ( InputStream fp = new FileInputStream(InfoFiledConstant.JSON_SRC))
        {
            byte[] bytes = fp.readAllBytes();
//            JSONPath path = JSONPath.of("$.MyPhone");
            //组装访问的json路径(内容)path
            JSONPath path = JSONPath.of("$.MyPhone");
            //读取字节码成Json格式
            JSONReader parse = JSONReader.of(bytes);
            JSONArray extract =(JSONArray) path.extract(parse);
            //result:ASUS
            System.out.println(extract.get(2));
        }catch (Exception e){
            e.printStackTrace();
        }

    }

其他:

    static {
        try (InputStream is = new FileInputStream(InfoFiledConstant.JSON_SRC)) {
            byte[] b = is.readAllBytes();
            s = new String(b);
        } catch (Exception e) {
            System.out.println("ErrorJsonStringRead:" + e.getMessage());
        }
    }

    private static Random r = new Random();
    private static String s;

    private static String common(String infoConstant) {
        String key;
        JSONArray common = JSONArray.parse(JSONObject.parseObject(s).get(infoConstant).toString());
        key = (String) common.get(r.nextInt(common.size()));
        return key;
    }

  • 11
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Fastjson是一个功能强大的Java库,可以方便地实现Java对象JSON字符串之间的换。要把Java List换成JSON格式,请按如下步骤操作: 1. 安装Fastjson:首先需要在项目中添加Fastjson的依赖。您可以通过Maven或Gradle等工具安装Fastjson。 2. 创建List:创建一个Java List,其中包含您希望换为JSON格式的数据。 3. 调用toJSONString:使用Fastjson的toJSONString方法将List换为JSON字符串。 示例代码: ``` import com.alibaba.fastjson.JSON; import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("item1"); list.add("item2"); list.add("item3"); String json = JSON.toJSONString(list); System.out.println(json); } } ``` 输出: ``` ["item1","item2","item3"] ``` 这就是如何使用FastjsonJava List换为JSON字符串的方法。 ### 回答2: 在Java中,可以使用Fastjson库将List数组换成JSON格式。 首先,需要导入Fastjson的库文件。 然后,创建一个List对象,例如: ```java List<String> list = new ArrayList<>(); list.add("A"); list.add("B"); list.add("C"); ``` 接下来,使用Fastjson的toJSONString方法将List对象换成JSON字符串: ```java String jsonString = JSON.toJSONString(list); ``` 最后,可以根据需要进行后续操作,例如打印输出或者发送到服务器等。 另外,如果List中存储的是自定义的对象,可以先将对象换成JSON对象,再将JSON对象添加到List中,然后进行换。例如: ```java List<User> userList = new ArrayList<>(); User user1 = new User("Alice", 20); User user2 = new User("Bob", 25); userList.add(user1); userList.add(user2); JSONArray jsonArray = new JSONArray(); for (User user : userList) { JSONObject jsonObject = new JSONObject(); jsonObject.put("name", user.getName()); jsonObject.put("age", user.getAge()); jsonArray.add(jsonObject); } String jsonString = jsonArray.toJSONString(); ``` 注意,使用Fastjson进行JSON换时,需要确保相关的对象有合适的getter和setter方法,以便进行序列和反序列操作。此外,还需要根据实际情况导入相关的Fastjson库文件。 ### 回答3: 使用fastjson将List数组换为JSON格式非常简单。首先,需要导入fastjson库。 请参考以下代码示例: ```java import com.alibaba.fastjson.JSON; import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { // 创建一个List数组 List<String> list = new ArrayList<>(); list.add("apple"); list.add("banana"); list.add("orange"); // 将List数组换为JSON格式字符串 String jsonString = JSON.toJSONString(list); System.out.println(jsonString); } } ``` 以上代码将输出以下结果: ``` ["apple","banana","orange"] ``` 在示例中,我们创建了一个List数组并添加了几个元素。然后,我们使用`JSON.toJSONString(list)`方法将List数组换为JSON格式的字符串。最后,我们通过`System.out.println(jsonString)`将结果打印出来。 需要注意的是,我们使用`com.alibaba.fastjson.JSON`类来实现JSON换。在使用fastjson之前,我们需要先下载fastjson库并将其添加到项目的classpath中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值