FastJson:JSONObject如何转JSONArray

使用 Fastjson 将 JSONObject 转换为 JSONArray 需要进行一些手动的步骤。

先构造JSONObject,然后将其添加到 JSONArray 中即可。

以下是一个示例代码片段,展示了如何进行这个转换:

java 复制代码
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSON;

public class Main {
    public static void main(String[] args) {
        // 创建一个 JSONObject
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("key1", "value1");
        jsonObject.put("key2", "value2");

        // 将 JSONObject 转为 JSONArray
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(jsonObject);

        // 打印结果
        System.out.println(jsonArray.toJSONString());
    }
}

是的,就是这么简单。不要再用什么先转JSON字符串,然后又用JSON对象parse来parse去的。

相关推荐
小白学大数据6 分钟前
Codex 里的 GPT 6 Astra、GPT 5.6 Sol、Terra、Luna 怎么选
开发语言·gpt·microsoft
l1t1 小时前
DeepSeek 4.1总结的Tom Lane 谈塑造 Postgres 三十年历程的架构决策
开发语言·数据库·postgresql·架构
欢迎来到祖安!2 小时前
企业微信 API 接口能力介绍:支持消息收发、图片发送、表情互动、联系人管理等多场景应用
java·前端·数据仓库·spring cloud·企业微信
dadaobusi6 小时前
学习:RV lkvm SBI
java·学习·spring
djarmy7 小时前
MAIN.c(1): warning C318: can’t open file ‘STC8G.H’ 报错 解决 分析
c语言·开发语言·mongodb
HEJOO98 小时前
深入理解 Java volatile 关键字:原理、用法与常见误区
java·开发语言·spring
曹牧8 小时前
Java:no content to map due to end of input
java·开发语言
阿维的博客日记8 小时前
Example 类全场景实战指南
java·mybatis
梦梦代码精8 小时前
《回收租赁系统技术选型避坑指南:业务闭环与二开自由度详解》
开发语言·低代码·docker·开源·代码规范
两点王爷8 小时前
SpringBoot 项目集成 GeoServer 源码,实现地图服务发布
java·spring boot·后端