Android - 解析没有数组名称的JSON数组

时间:2017-01-25 13:44:29

标签: android arrays json

JSON LInk - http://jsonviewer.stack.hu/#http://www.saveme.ie/api/savings/

我试图通过这个后台任务获取一个JSON来填充Android中的listview,但是它说JSON存在问题。这似乎是因为数组没有名称。

我如何修改代码以便它可以获取没有名称的数组?

private class GetSavings extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        HttpHandler sh = new HttpHandler();

        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(url);

        Log.e(TAG, "Response from url: " + jsonStr);

        if (jsonStr != null) {
            try {
                **JSONObject jsonObj = new JSONObject(jsonStr);

                // Getting JSON Array node
                JSONArray savings = jsonObj.getJSONArray("savings");
                // looping through All Contacts
                for (int i = 0; i < savings.length(); i++) {
                    JSONObject c = savings.getJSONObject(i);
                    String title = c.getString("title");

                }**
            } catch (final JSONException e) {
                Log.e(TAG, "Json parsing error: " + e.getMessage());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(),
                                "Json parsing error: " + e.getMessage(),
                                Toast.LENGTH_LONG)
                                .show();
                    }
                });

            }
        } else {
            Log.e(TAG, "Couldn't get json from server.");
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(),
                            "Couldn't get json from server. Check LogCat for possible errors!",
                            Toast.LENGTH_LONG)
                            .show();
                }
            });

        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        // Dismiss the progress dialog
        if (pDialog.isShowing())
            pDialog.dismiss();
        /**
         * Updating parsed JSON data into ListView
         * */
        ListAdapter adapter = new SimpleAdapter(
                MainActivity.this, savingsList,
                R.layout.list_item, new String[]{"name", "email",
                "mobile"}, new int[]{R.id.name,
                R.id.email, R.id.mobile});

        lv.setAdapter(adapter);
    }

}

1 个答案:

答案 0 :(得分:1)

而不是将其投放到JSONObject中,而是将其投放到JSONArray

JSONArray jsonObj = new JSONArray(jsonStr);

然后遍历你的数组。