使用Java内置的JSON库orgjson写个解析取字符串demo加上抛出异常
import org.json.*;
public class JSONParserDemo { public static void main(String[] args) { String jsonString = "{"name":"John","age":30,"city":"New York"}";
try {
JSONObject jsonObject = new JSONObject(jsonString);
String name = jsonObject.getString("name");
int age = jsonObject.getInt("age");
String city = jsonObject.getString("city");
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("City: " + city);
} catch (JSONException e) {
System.out.println("Error parsing JSON string: " + e.getMessage());
}
}
原文地址: https://www.cveoy.top/t/topic/fFcD 著作权归作者所有。请勿转载和采集!