使用java处理json字符串

package nj.zb.kb22;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class DataProcessJson {
    public static void main(String[] args) {
        String str = "{\n" +
                "  \"employees\": [\n" +
                "    {\n" +
                "      \"firstName\": \"John\",\n" +
                "      \"lastName\": \"Doe\",\n" +
                "      \"email\": \"john.doe@example.com\",\n" +
                "      \"age\": 30,\n" +
                "      \"address\": {\n" +
                "        \"street\": \"123 Main St\",\n" +
                "        \"city\": \"New York\",\n" +
                "        \"state\": \"NY\",\n" +
                "        \"postalCode\": \"10001\"\n" +
                "      },\n" +
                "      \"skills\": [\"Java\", \"Python\", \"SQL\"]\n" +
                "    },\n" +
                "    {\n" +
                "      \"firstName\": \"Jane\",\n" +
                "      \"lastName\": \"Smith\",\n" +
                "      \"email\": \"jane.smith@example.com\",\n" +
                "      \"age\": 28,\n" +
                "      \"address\": {\n" +
                "        \"street\": \"456 Elm St\",\n" +
                "        \"city\": \"San Francisco\",\n" +
                "        \"state\": \"CA\",\n" +
                "        \"postalCode\": \"94101\"\n" +
                "      },\n" +
                "      \"skills\": [\"JavaScript\", \"HTML\", \"CSS\"]\n" +
                "    }\n" +
                "  ],\n" +
                "  \"company\": \"ABC Corporation\",\n" +
                "  \"website\": \"www.example.com\"\n" +
                "}";
        StringBuilder stringBuilder = new StringBuilder();
        BufferedWriter bufferedWriter = null;
        try {
            JSONObject jsonObject = new JSONObject(str);
            JSONArray employees = jsonObject.getJSONArray("employees");
            for (int i = 0; i < employees.length(); i++) {
                JSONObject jsonObject1 = employees.getJSONObject(i);
                String firstname = jsonObject1.getString("firstName");
                StringBuilder append = stringBuilder.append(firstname).append("\\n\\n");
                //System.out.println(firstname);
                bufferedWriter = new BufferedWriter(new FileWriter("D:\\KB22f\\nj\\zb\\javaspark\\.idea\\text\\text.txt"));
                bufferedWriter.write(String.valueOf(append));
            }
            System.out.println(stringBuilder);
        } catch (JSONException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (bufferedWriter != null) {
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }//main
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值