You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java解析JSON文件:将控制台输出改为指定结构的JSONObject

改造JSON解析代码生成指定格式输出

现有基于json-simple库的Java代码用于解析本地details.json文件,原代码会将解析出的success状态、csrf的value值及data数组中的字段逐行打印到控制台。现需修改代码,使其输出符合指定结构的JSONObject,包含statusCode、value和data数组。

现有代码

package com.example.demo.service;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class Task2 {
    public static void main(String[] args) throws FileNotFoundException, IOException, ParseException {
        JSONParser jp = new JSONParser();
        Object ob = jp.parse(new FileReader("C:\\Users\\z033055\\Downloads\\details.json"));
        JSONObject jo = (JSONObject) ob;
        boolean success = (boolean) jo.get("success");
        System.out.println("success:" + success);
        JSONObject csrf = (JSONObject) jo.get("csrf");
        String value = (String) csrf.get("value");
        System.out.println("value:" + value);
        JSONArray data = (JSONArray) jo.get("data");
        for (int i = 0; i < data.size(); i++) {
            JSONObject jdata = (JSONObject) data.get(i);
            String id = (String) jdata.get("id");
            System.out.println("id:" + id);
            String type = (String) jdata.get("type");
            System.out.println("type:" + type);
            String identifier = (String) jdata.get("identifier");
            System.out.println("identifier:" + identifier);
            String cestamp = (String) jdata.get("cestamp");
            System.out.println("cestamp:" + cestamp);
        }
    }
}

当前输出

success:true
value:MLGY-SWOI-GHDD-LEDF-TSO7-JV8X-XIEC-0TQ5
id:B65C2473BE580000622048BE000EEC0B
type:ECUParameterValueInteger__36e72ec09f5d4b9ba4e6da8d8d393941
identifier:B65C2473BE580000622048BE000EEC0B
cestamp:B65C2473BE580000622051FB001D83E0

期望输出

{
    "statusCode":"",
    "value":"",
    "data": [{
        "id": "B65C2473BE580000622048BE000EEC0B",
        "type": "ECUParameterValueInteger__36e72ec09f5d4b9ba4e6da8d8d393941",
        "identifier": "B65C2473BE580000622048BE000EEC0B",
        "cestamp": "30B841C5DC52000062468FC2001202DA"
    } ]
}

修改后的代码

package com.example.demo.service;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.json.simple.JSONValue;

public class Task2 {
    public static void main(String[] args) throws FileNotFoundException, IOException, ParseException {
        JSONParser jp = new JSONParser();
        Object ob = jp.parse(new FileReader("C:\\Users\\z033055\\Downloads\\details.json"));
        JSONObject originalJson = (JSONObject) ob;

        // 创建用于输出的目标JSON对象
        JSONObject outputJson = new JSONObject();
        
        // 设置statusCode字段(示例中为空字符串,可根据success状态调整为对应码值)
        outputJson.put("statusCode", "");
        
        // 提取并设置csrf的value值
        JSONObject csrf = (JSONObject) originalJson.get("csrf");
        String value = (String) csrf.get("value");
        outputJson.put("value", value);
        
        // 复用原data数组
        JSONArray data = (JSONArray) originalJson.get("data");
        outputJson.put("data", data);
        
        // 格式化输出JSON字符串
        System.out.println(JSONValue.prettyPrint(outputJson));
    }
}

修改说明

  1. 新增outputJson对象组装目标输出结构,替代原逐行打印逻辑
  2. 按需求添加statusCode字段,可根据原success状态设置对应业务码值,比如success=true时设为"200"
  3. 提取原JSON中的csrf.value并放入输出对象
  4. 直接复用原JSON中的data数组,无需逐个字段遍历
  5. 使用JSONValue.prettyPrint()生成格式化的JSON字符串,与期望输出格式一致

内容的提问来源于stack exchange,提问作者Praveen s

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 13:18:26