10分钟掌握XML、JSON及其解析(6)

private String createJson() throws JSONException { JSONObject jsonObject = new JSONObject(); jsonObject.put("intKey", 123); jsonObject.put("doubleKey", 10.1); jsonObject.put("longKey", 666666666); jsonObject.put("stringKey", "lalala"); jsonObject.put("booleanKey", true); JSONArray jsonArray = new JSONArray(); jsonArray.put(0, 111); jsonArray.put("second"); jsonObject.put("arrayKey", jsonArray); JSONObject innerJsonObject = new JSONObject(); innerJsonObject.put("innerStr", "inner"); jsonObject.put("innerObjectKey", innerJsonObject); Log.e("Json", jsonObject.toString()); return jsonObject.toString(); }

其输出结果如下所示:

{"intKey":123, "doubleKey":10.1, "longKey":666666666, "stringKey":"lalala", "booleanKey":true, "arrayKey":[111,"second"], "innerObjectKey":{"innerStr":"inner"}}

<3>. 如何解析JSON?

下面以视频中解析iQiyi的每个视频album数据为例来说明如何解析JSON:

第一步,需要从网络服务器上发起请求,获取到JSON数据:

JsonObjectRequest jsonObjRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { try { MyLog.d(TAG, "response=" + response); parseiQiyiInterfaceResponse(response); } catch (Exception e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { /* * if (error instanceof NetworkError) { } else if (error * instanceof ClientError) { } else if (error instanceof * ServerError) { } else if (error instanceof * AuthFailureError) { } else if (error instanceof * ParseError) { } else if (error instanceof * NoConnectionError) { } else if (error instanceof * TimeoutError) { } */ MyLog.e(TAG, "onErrorResponse, error=" + error); } }) { @Override public Map<String, String> getHeaders() throws AuthFailureError { HashMap<String, String> headers = new HashMap<String, String>(); headers.put("t", iQiyiInterface.getEncryptTimestamp()); headers.put("sign", iQiyiInterface.getSign()); return headers; } };

第二步,获取到对应的对应的JSONObject数据:

public void getJsonObjectString(String url) { mQueue = VideoApplication.getInstance().getRequestQueue(); JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { MyLog.e(TAG, "response = " + response.toString()); JSONArray jsonArray = null; JSONObject jsonObject = null; try { jsonObject = response.getJSONObject("response"); jsonArray = jsonObject.getJSONObject("result").getJSONArray("album"); } catch (JSONException e) { e.printStackTrace(); } if (jsonArray == null) { return; } mChannelList = VideoUtils.parseVideoJsonArray(jsonArray); if (isLoading) { isLoading = false; if (mIsGrid) { mChannelGridAdapter.appendChannelVideoInfo(mChannelList); } else { mChannelListAdapter.appendChannelVideoInfo(mChannelList); } } else { if (mIsGrid) { mChannelGridAdapter.setChannelVideoInfo(mChannelList); showOppoGrid(); } else { mChannelListAdapter.setChannelVideoInfo(mChannelList); showOppoList(); } } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { MyLog.e(TAG, "error = " + error); } }); jsObjRequest.setTag(TAG); jsObjRequest.setShouldCache(true); mQueue.add(jsObjRequest); mQueue.start(); }

获取到JSON Object之后,就对这个JSONObject进行解析:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzdysx.html