没有JSON键的值

时间:2018-05-28 15:14:02

标签: android json

我正在尝试解析这个json

    {
      "data": [
    {  "name": "4th Floor",
          "is_broken": true,
          "is_repaired": false,
          "is_ok": false,
          "asset_parent": {
            "name": "Buni Building",
            "is_broken": true,
            "is_repaired": false,
            "is_ok": false
          }
        }
      ]
}

使用此代码

 class daftarAset extends AsyncTask<String, String, String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Sedang menampilkan...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected String doInBackground(String... params) {
        String link_url = "https://example.com/api/assets";
        HttpHandler sh = new HttpHandler();
        String json = sh.makeServiceCall(link_url);

        Log.e(TAG, "Response from url: " + json);
            if (json != null) {
                try {
                   JSONObject jsonObject = new JSONObject();
                    JSONArray data = jsonObject.getJSONArray("data");
                for (int i = 0; i < data.length(); i++) {
                    JSONObject ar = data.getJSONObject(i);
                    String aset = ar.getString("name");

                    JSONObject parent = ar.getJSONObject("asset_parent");
                    String nama = parent.getString("name");
                    HashMap map = new HashMap();
                    map.put(in_aset, aset);
                    map.put(in_ruang, nama);
                    data_map.add(map);
                }
            } catch (final JSONException e) {
                    Log.e(TAG, "Json parsing error: " + e.getMessage());
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getActivity().getApplicationContext(),
                                    "Json parsing error: " + e.getMessage(),
                                    Toast.LENGTH_LONG).show();
                        }
                    });
                }
                getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    list = (ListView) getView().findViewById(R.id.baik_list);
                    adapter = new AssetsAdapter(getActivity(), data_map);
                    list.setAdapter(adapter);
                    setListViewHeightBasedOnChildren(list);

                }
            });
        }
        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        pDialog.dismiss();
    }

它表示错误没有数据值。我不知道为什么数据说没有我以前用JSONArray调用它的值。它必须与我必须放置值的列表视图无关。 请帮助为什么它说没有数据的价值,但它实际上有

1 个答案:

答案 0 :(得分:3)

您收到错误是因为您没有将json字符串传递给json对象

应该是

JSONObject jsonObject = new JSONObject(json);

而不是

JSONObject jsonObject = new JSONObject(); //json is missing here