3 回答
TA贡献1725条经验 获得超7个赞
您的 JSON 有效。
使用下面的代码来解析您的 JSON。
try {
JSONObject obj = new JSONObject(json);
categoryGetSet temp;
JSONObject resultObject = (JSONObject) obj.get("result");
String txt_title = resultObject.getString("title");
String txt_provider = resultObject.getString("provider");
JSONArray ja_video = resultObject.getJSONArray("link");
for (int i = 0; i < ja_video.length(); ++i) {
temp = new categoryGetSet();
JSONObject jo_data = ja_video.getJSONObject(i);
String txt_link_name = jo_data.getString("link_name");
String txt_video = jo_data.getString("video_link");
temp.setTitle(txt_title);
temp.setLinkName(txt_link_name);
temp.setImage(txt_provider);
temp.setVideo(txt_video);
Log.e("jsonResponse: ", "txt_title" + txt_title);
Log.e("jsonResponse: ", "txt_link_name" + txt_link_name);
Log.e("jsonResponse: ", "txt_provider" + txt_provider);
Log.e("jsonResponse: ", "txt_video" + txt_video);
}
} catch (JSONException e) {
e.printStackTrace();
Log.e("catch", e.getLocalizedMessage());
}
TA贡献1831条经验 获得超10个赞
因为链接数组位于结果对象内部,但您试图从外部对象获取,所以您会收到异常。
尝试这样解析
JSONObject obj = new JSONObject(response); JSONObject resultObj=obj.getJSONObject("result"); JSONArray ja_video = resultObj.getJSONArray("link");
TA贡献1946条经验 获得超4个赞
它会陷入困境,因为你试图以错误的方式解析,
你需要按顺序传递每个 JSON 对象和数组,波纹管是你的结构
主要 Json 对象
链接数组
带有数据的子对象
结果对象
另一个错误是你正在尝试一些不存在的密钥
尝试下面的代码它会对你有帮助
StringRequest stringRequest = new StringRequest(Request.Method.GET, hp,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//hiding the progressbar after completion
Log.e("Response", response);
try {
JSONObject mainObject = new JSONObject(response);
JSONObject resultObj=mainObject.getJSONObject("result");
JSONArray linkJsonArrya = resultObj.getJSONArray("link");
categoryGetSet temp;
for (int i = 0; i < linkJsonArrya.length(); i++) {
temp = new categoryGetSet();
JSONObject jo_data = linkJsonArrya.getJSONObject(i);
if(jo_data.has("title")){
temp.setTitle(jo_data.getString("title"));
}else {
temp.setTitle(jo_data.getString(""));
}
if(jo_data.has("link_name")){
temp.setLinkName(jo_data.getString("link_name"));
}else {
temp.setLinkName(jo_data.getString(""));
}
if(jo_data.has("img_link")){
temp.setImage(jo_data.getString("img_link"));
}else {
temp.setImage(jo_data.getString(""));
}
if(jo_data.has("video_link")){
temp.setVideo(jo_data.getString("video_link"));
}else {
temp.setVideo(jo_data.getString(""));
}
categoryGetSets.add(temp);
}
Log.e("try", "try");
} catch (JSONException e) {
e.printStackTrace();
Log.e("catch", "catch");
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
NetworkResponse networkResponse = error.networkResponse;
if (networkResponse != null) {
Log.e("Status code", String.valueOf(networkResponse.statusCode));
Toast.makeText(getActivity().getApplicationContext(), String.valueOf(networkResponse.statusCode), Toast.LENGTH_SHORT).show();
}
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getActivity().getApplicationContext());
requestQueue.add(stringRequest);
}
添加回答
举报