Java org.json 解析 JSON 字符串示例,包含异常处理
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/ok4L 著作权归作者所有。请勿转载和采集!