之前解析json一直用get的方法,但是如果抛异常会让解析中断很烦人。今天发现了JSONObject还提供了一个更好用的方法opt。看来以后文档还是要更加认真的读的,下面是文档中的原文。
A JSONObject constructor can be used to convert an external form JSON text into an internal form whose values can be retrieved with the@H_404_9@getand@H_404_9@optmethods,or to convert values into a JSON text using the@H_404_9@putand@H_404_9@toStringmethods. A@H_404_9@getmethod returns a value if one can be found,and throws an exception if one cannot be found. An@H_404_9@optmethod returns a default value instead of throwing an exception,and so is useful for obtaining optional values.
详见:http://www.json.org/javadoc/org/json/JSONObject.html
原文链接:https://www.f2er.com/json/290122.html