PIPIONE
您在问题中提供的不是格式良好的 json。忽略这一点,您可以将格式正确的 json 字符串读取到 JSON 对象中,并根据需要替换值:private static String getData(String name, int age, String street, String line) throws JSONException { JSONObject jsonObject = new JSONObject("{ name : VARIABLE1, age : VARIABLE2, address : { street : VARIABLE3, line : VARIABLE4 }}"); JSONObject address = (JSONObject) jsonObject.get("address"); jsonObject.put("name", name); jsonObject.put("age", age); address.put("street", street); address.put("line", line); return jsonObject.toString();}您可以将此方法调用为:getData("Random", 20, "str", "lin");