简体   繁体   English

将json响应存储在数组列表中

[英]store json response in array list

error:value passed: ("contact") not found. 错误:未通过值传递:(“联系人”)。 given code is to get json format data in array list.i'm doing this task in android studio. 给定的代码是在数组列表中获取json格式的数据。我正在android studio中执行此任务。 when i run the below code, i could see the json format data with error that "contact" not found.i have change that with obj and other declared variable,but still facing same problem. 当我运行下面的代码时,我可以看到json格式的数据,但未找到“ contact”错误。我已将obj和其他声明的变量进行了更改,但仍然面临相同的问题。 any suggestion to resolve this? 有什么解决的建议吗?
here is the code: 这是代码:

 private String TAG = MainActivity.class.getSimpleName();

private ProgressDialog pDialog;
private ListView lv;

// URL to get contacts JSON
private static String url = "http://services.groupkt.com/state/get/IND/all";

List<JsonBean> listobj = new ArrayList<>();

//  ArrayList<HashMap<String, String>> contactList;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

  //lv = (ListView) findViewById(R.id.list);

    new GetContacts().execute();
}

private class GetContacts 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) {
        Httphanlder sh = new Httphanlder();

        // 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 contacts = jsonObj.getJSONArray("contacts");

               JsonBean jsonbeanobj=new JsonBean();

                // looping through All Contacts
                for (int i = 0; i < contacts.length(); i++) {
                    JSONObject c = contacts.getJSONObject(i);

                    jsonbeanobj.setCountry(c.getString("country"));
                    jsonbeanobj.setName(c.getString("name"));
                    jsonbeanobj.setAbbr(c.getString("abbr"));
                    jsonbeanobj.setArea(c.getString("area"));
                    jsonbeanobj.setCapital(c.getString("capital"));

                    listobj.add(jsonbeanobj);
                  }
            } 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();

    }

}

This might not be an answer to that problem, but usually when I see people using JSONObject..etc they are new to programming. 这可能不是该问题的答案,但是通常当我看到人们使用JSONObject..etc时,他们是编程的新手。 I used to do the same :) 我曾经做过同样的事情:)

Have a look at Gson or Jackson Library, it will deserialise the json to a java object for you. 看看Gson或Jackson库,它将为您将json反序列化为Java对象。

Then in that example you can do this using Jackson: 然后在该示例中,您可以使用Jackson进行以下操作:

List contacts = Array.asList(mapper.readValue(json, Contact[].class));

public class Contact {
    @JsonCreator
    public Contact(@JsonProperty("name") name...etc) 
      this.name = name; 
    }
}

You need to use json array "result" 您需要使用json数组“结果”

update this line 更新这行

// Getting JSON Array node
JSONArray contacts = jsonObj.getJSONArray("result");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM