import org.json.JSONObject;
public class JsonStringToObjectExample {
public static void main(String[] args) {
// 定义一个JSON字符串
String jsonString = "{\"name\":\"John\", \"age\":30, \"city\":\"New York\"}";
// 将JSON字符串转换为JSONObject对象
JSONObject jsonObject = new JSONObject(jsonString);
// 输出JSON对象中的数据
System.out.println("Name: " + jsonObject.getString("name"));
System.out.println("Age: " + jsonObject.getInt("age"));
System.out.println("City: " + jsonObject.getString("city"));
}
}
org.json.JSONObject
类来处理 JSON 数据。确保你已经添加了相应的库(如 json-simple
或 org.json
)到你的项目中。jsonString
。new JSONObject(jsonString)
将 JSON 字符串转换为 JSONObject
对象。jsonObject.getString("key")
和 jsonObject.getInt("key")
等方法获取 JSON 对象中的值。上一篇:java生成uuid方法
下一篇:java classpath
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站