json解析常见问题:
-
getJSONObject与optJSONObject的区别,下面结合源码和案例来分析当我们使用这两周方法来解析数据时,哪种比较好.
-
源码分析:
<code class="hljs java has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//使用getJSONObject时,如果返回的对象不是JSONObject,抛出JSONException异常</span> <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** * Returns the value mapped by {@code name} if it exists and is a {@code * JSONObject}. *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @throws</span> JSONException if the mapping doesn't exist or is not a {@code * JSONObject}. */</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> JSONObject <span class="hljs-title" style="box-sizing: border-box;">getJSONObject</span>(String name) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">throws</span> JSONException { Object object = get(name); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (object <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">instanceof</span> JSONObject) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> (JSONObject) object; } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">throw</span> JSON.typeMismatch(name, object, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"JSONObject"</span>); } } <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//使用optJSONObject时,当返回结果不是JSONObject时,这里不会抛异常,而是返回null</span> <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** * Returns the value mapped by {@code name} if it exists and is a {@code * JSONObject}. Returns null otherwise. */</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> JSONObject <span class="hljs-title" style="box-sizing: border-box;">optJSONObject</span>(String name) { Object object = opt(name); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> object <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">instanceof</span> JSONObject ? (JSONObject) object : <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>; } </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li></ul>
- 结合项目代码分析使用场景
<code class="hljs java has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"> <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** * 伪代码如下 **/</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">GetGoodsCollectListFactory</span> {</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> String ERROR_MSG = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"errorMsg"</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> String ERROR_CODE = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"errorCode"</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> String RESULT = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"result"</span>; <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** * 商品列表,解析获取到的json数据 * *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @param</span> param *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @return</span> */</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> CollectListInfo <span class="hljs-title" style="box-sizing: border-box;">getGoodsCollectListParse</span>(String param) { String BOOK_MARK_BO_LIST = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"favorGoodsList"</span>; CollectListInfo collectList = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> CollectListInfo(); List<CollectInfo> list = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> ArrayList<CollectInfo>(); ResponseInfo responseInfo = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>; CollectInfo info = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">try</span> { <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 请求获取json结果</span> JSONObject jsonObject = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> JSONObject(HttpRequestClient.executeRequest(Constant.Url.GET_FOLLOW_GOODS_URL, param, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>)); responseInfo = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> ResponseInfo(); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (jsonObject.getBoolean(RESULT)) { responseInfo.setResult(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>); JSONArray jsonArray = jsonObject.getJSONArray(BOOK_MARK_BO_LIST); collectList.setTotalPage(jsonObject.getInt(Constant.TOTAL_PAGE_NAME)); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; i < jsonArray.length(); i++) { info = collectList.getCollectInfo(); SONObject object = jsonArray.getJSONObject(i); JSONArray activityItemList = object.optJSONArray(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"activityItemList"</span>); ArrayList<ActivityCollectInfo> activityCollectInfos = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> ArrayList<ActivityCollectInfo>(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//因为object.optJSONArray("activityItemList");如果解析不到会直接返null</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (activityItemList != <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span> && activityItemList.size() > <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> j = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; j < activityItemList.length(); j++) { <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//使用optJSONObject(i)会莫名解析不了下面数据:如optString("activityId"));</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 当activityid为null时,不会抛错,但是下面的方法不走,因此换用getJSONObject(i);</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//JSONObject activityItemobj = activityItemList.optJSONObject(i);</span> JSONObject activityItemobj = activityItemList.getJSONObject(i); ActivityCollectInfo activityCollectInfo = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> ActivityCollectInfo(); activityCollectInfo.setActivityId(activityItemobj.optString(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"activityId"</span>)); activityCollectInfo.setActivityName(activityItemobj.optString(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"activityName"</span>)); activityCollectInfo.setActivityType(activityItemobj.optInt(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"activityType"</span>)); activityCollectInfo.setLargessFlag(activityItemobj.optInt(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"largessFlag"</span>)); activityCollectInfo.setLargessMoney(activityItemobj.optInt(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"largessMoney"</span>)); activityCollectInfo.setLargessNumber(activityItemobj.optInt(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"largessNumber"</span>)); activityCollectInfos.add(activityCollectInfo); } ... } }</code>