import com.alibaba.fastjson.*;
public class JsonParser {
public static void main(String[] args) {
String jsonStr = "{\"name\":\"John\", \"age\":30}"; // JSON字符串示例
// 将JSON字符串转换为JSONObject对象
JSONObject jsonObj = JSON.parseObject(jsonStr);
// 获取指定键名的值
String name = jsonObj.getString("name");
int age = jsonObj.getIntValue("age");
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
Gson解析的使用
String json = "{\"name\":\"John\",\"age\":30}";Person person = gson.fromJson(json, Person.class);