解析android

时间:2016-07-04 12:31:54

标签: android json parsing

我正在尝试显示从json数据源接收的数据。但是当我尝试解析数据时,我收到错误。我无法弄清楚如何解决它。

json数据看起来像这样:

click here to view

这是我为解析编写的函数:

public void makeJSONRequest() {
    JsonArrayRequest req = new JsonArrayRequest(endpoint_final,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());
                    // UI
                    swipeRefreshLayout.setRefreshing(false);
                    for (int i = 0; i < response.length(); i++) {
                        try {
                            JSONObject object = response.getJSONObject(i);
                            JSONObject newsItems = object.getJSONObject("newsItems");
                            for (int j = 0; j < newsItems.length(); j++) {
                                titles[j] = newsItems.getString("title");
                                snips[j] = newsItems.getString("snip");
                                links[j] = newsItems.getString("link");
                            }

                        } catch (JSONException e) {
                            Log.e(TAG, "Json parsing error: " + e.getMessage());
                            Toast.makeText(getActivity(), "JSON Parsing error", Toast.LENGTH_LONG).show();
                        }
                    }
                    mAdapter.notifyDataSetChanged();
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "Error: " + error.getMessage());
            Toast.makeText(getContext(), "Error Receiving News", Toast.LENGTH_LONG).show();
        }
    });

    // Adding request to request queue
    InitializeVolley.getInstance().addToRequestQueue(req);
}

我得到的错误是:

  

错误:org.json.JSONException:Value {&#34; newsItems&#34;:[{&#34; link&#34;:&#34; http://www.srmuniv.ac.in/announcement / special-training-details&#34;,&#34; snip&#34;:&#34;亲爱的学生,对安置感兴趣的人必须遵循某些指导原则。您必须清除CDC进行的测试,只有那些。 ..&#34;,&#34; title&#34;:&#34; CDC:特殊培训 - 更新&#34;} ....

任何帮助将不胜感激。 谢谢!

6 个答案:

答案 0 :(得分:1)

首先,这个json不是一个数组,它是一个对象。您应该使用JsonObjectRequest而不是JsonArrayRequest。你错误地解析了json。

public void makeJSONRequest() {
JsonObjectRequest req = new JsonObjectRequest(endpoint_final,
        new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Log.d(TAG, response.toString());
                // UI
                swipeRefreshLayout.setRefreshing(false);
                JSONArray newsAr = response.getJsonArray("news");
                for (int i = 0; i < newsAr.length(); i++) {
                    try {
                        JSONObject newsItem = newsAr.getJSONObject(i);
                        titles[j] = newsItem.getString("title");
                        snips[j] = newsItem.getString("snip");
                        links[j] = newsItem.getString("link");

                    } catch (JSONException e) {
                        Log.e(TAG, "Json parsing error: " + e.getMessage());
                        Toast.makeText(getActivity(), "JSON Parsing error", Toast.LENGTH_LONG).show();
                    }
                }
                mAdapter.notifyDataSetChanged();
            }
        }, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Log.e(TAG, "Error: " + error.getMessage());
        Toast.makeText(getContext(), "Error Receiving News", Toast.LENGTH_LONG).show();
    }
});

// Adding request to request queue
InitializeVolley.getInstance().addToRequestQueue(req);
}

答案 1 :(得分:1)

JSONObject newsItems = object.getJSONObject("newsItems");

替换为

JSONArray newsItems = object.getJSONArray("newsItems");

答案 2 :(得分:0)

JSONObject object =new JSONObject[response];
JSONArray newsItems = object.getJSONArray("newsItems");

for (int j = 0; j < newsItems.length(); j++) {

    JsonObject internal=newsItems.getJsonObject(j);
    titles[j] = internal.getString("title");
    snips[j] = internal.getString("snip");
    links[j] = internal.getString("link");
}

使用JSONArray而不是JSONObjectnewsItemsJsonArray

答案 3 :(得分:0)

试试这个:

public void makeJSONRequest() {
    JsonArrayRequest req = new JsonArrayRequest(endpoint_final,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());
                    // UI
                    swipeRefreshLayout.setRefreshing(false);
                    jo = new JSONObject(response);;
                    JSONArray array = jo.getJSONArray("newsItems");
                    for (int x = 0; x < array.length(); x++) {
                        try {
                            titles[j] = array.getJSONObject(x).optString("title");
                            snips[j]= array.getJSONObject(x).optString("snip");
                            links[j] = array.getJSONObject(x).optString("link");
                        } catch (JSONException e) {
                            Log.e(TAG, "Json parsing error: " + e.getMessage());
                            Toast.makeText(getActivity(), "JSON Parsing error", Toast.LENGTH_LONG).show();
                        }
                    }
                    mAdapter.notifyDataSetChanged();
                }
            }, 
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e(TAG, "Error: " + error.getMessage());
                    Toast.makeText(getContext(), "Error Receiving News", Toast.LENGTH_LONG).show();
                }
            }
    );

    // Adding request to request queue
    InitializeVolley.getInstance().addToRequestQueue(req);
}

答案 4 :(得分:0)

您可以(并且绝对应该)使用一个为您提供繁重工作的库,而不是编写您自己的解析函数。一个例子是Google的Gson库,请查看:https://github.com/google/gson

答案 5 :(得分:0)

解析如下:

try {
    JSONObject objResponse = new JSONObject(response.toString());
    JsonArray arrResponse = objResponse.getJSONArray("newsItems");
    for (int i = 0; i < arrResponse.size(); i++) {
        JSONObject objItems = arrResponse.get(i);
        String link = objItems.getString("link");
    }
}catch (Exception e){
    e.printStackTrace();
}