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

Java 在数组列表中存储json响应,java,json,android-studio,arraylist,Java,Json,Android Studio,Arraylist,错误:传递的值:(“联系人”)未找到。 给出的代码是在数组列表中获取json格式的数据。我在android studio中完成这个任务。当我运行下面的代码时,我可以看到json格式的数据,错误是“contact”没有找到。我已经用obj和其他声明的变量更改了它,但仍然面临同样的问题。 有什么解决这个问题的建议吗? 代码如下: private String TAG = MainActivity.class.getSimpleName(); private ProgressDialog pDia

错误:传递的值:(“联系人”)未找到。 给出的代码是在数组列表中获取json格式的数据。我在android studio中完成这个任务。当我运行下面的代码时,我可以看到json格式的数据,错误是“contact”没有找到。我已经用obj和其他声明的变量更改了它,但仍然面临同样的问题。 有什么解决这个问题的建议吗?
代码如下:

 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();

    }

}
private String TAG=MainActivity.class.getSimpleName();
私人对话;
私有ListView lv;
//获取联系人JSON的URL
专用静态字符串url=”http://services.groupkt.com/state/get/IND/all";
List listobj=new ArrayList();
//ArrayList联系人列表;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//lv=(ListView)findViewById(R.id.list);
新建GetContacts().execute();
}
私有类GetContacts扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
//显示进度对话框
pDialog=新建进度对话框(MainActivity.this);
setMessage(“请稍候…”);
pDialog.setCancelable(假);
pDialog.show();
}
@凌驾
受保护的Void doInBackground(Void…arg0){
Httphanlder sh=新的Httphanlder();
//向url发出请求并获得响应
字符串jsonStr=sh.makeServiceCall(url);
Log.e(标签,“来自url的响应:+jsonStr”);
if(jsonStr!=null){
试一试{
JSONObject jsonObj=新的JSONObject(jsonStr);
//获取JSON数组节点
JSONArray contacts=jsonObj.getJSONArray(“contacts”);
JsonBean jsonbeanobj=新的JsonBean();
//通过所有触点循环
对于(int i=0;i
这可能不是这个问题的答案,但通常当我看到人们使用JSONObject..等时,他们是编程新手。我以前也这么做过:)

看看Gson或Jackson库,它会将json反序列化为java对象

在该示例中,您可以使用Jackson执行此操作:

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

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

您需要使用json数组“result”

更新此行

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

仍然面临同样的问题,Json解析错误:结果没有值