如何使用AQuery从url解析带有对象类的Json数据

时间:2017-12-25 07:37:42

标签: android json aquery

这是我的代码,我在解析来自对象的数据时遇到错误,可以进行哪些更改以解析数据......

     aQuery.ajax(fetch_url, JSONObject.class, new AjaxCallback<JSONObject>(){
        @Override
        public void callback(String url, JSONObject obj, AjaxStatus status){
            super.callback(url, obj, status);
            Log.i("response", url + "response:" + obj);

            ArrayList<UserInfo> list = new ArrayList<>();


                try {
                    JSONObject jsonObject = new JSONObject();
                    JSONArray jsonArray = jsonObject.getJSONArray("org_list");

                    for (int i = 0; i < jsonArray.length(); i++) {

                    JSONObject object = jsonArray.getJSONObject(i);
                    UserInfo info = new UserInfo();
                    info.id = object.getString("Id");
                    info.servicename = object.getString("name");
                    info.amount = object.getString("amount");
                    list.add(info);
    });

}

这是我的JSON数据格式

{
  "org_list": [
    {
      "Id": "1",
      "name": "CBC-Test",
      "amount": "200"
    }
  ]
}

当我从下面的代码编辑时,现在我面临空响应值。我还附上了我的logcat图像文件,以获取有关我的问题的更多详细信息:Click here

Click here for further more details in my code

Logcat View1

Logcat View2

2 个答案:

答案 0 :(得分:0)

编辑您的代码,如下所示

aQuery.ajax(fetch_url, JSONObject.class, new AjaxCallback<JSONObject>() {
    @Override
    public void callback(String url, JSONObject obj, AjaxStatus status) {
        super.callback(url, obj, status);
        Log.i("response", url + "response:" + obj);

        ArrayList<UserInfo> list = new ArrayList<>();


        try {
            JSONArray jsonArray = obj.getJSONArray("org_list");

            for (int i = 0; i < jsonArray.length(); i++) {

                JSONObject object = jsonArray.getJSONObject(i);
                UserInfo info = new UserInfo();
                info.id = object.getString("Id");
                info.servicename = object.getString("name");
                info.amount = object.getString("amount");
                list.add(info);
            }
        } catch (Exception e){
            e.printStackTrace();
        }
    }

});

无需重新创建JsonObject的对象。只需使用响应即可。

答案 1 :(得分:0)

最后,我搜索了自己并找到了解决方案。但它没有使用AQuery,而是使用RequestQueue ...

    txtview = findViewById(R.id.showdata);
    Button buttonParse = findViewById(R.id.showbtn);

    requestQueue = Volley.newRequestQueue(this);

    buttonParse.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            jsonParse();
        }
    });
}
public void jsonParse(){

    String fetchurl = "http://use your url here.com/";

    JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, fetchurl, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {

            try {

                JSONArray jsonArray = response.getJSONArray("org_list");

                for (int i=0; i < jsonArray.length(); i++){

                    JSONObject patient = jsonArray.getJSONObject(i);

                    String firstName = patient.getString("orga_organame");
                    txtview.append(firstName+","+"\n\n");

                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    });

    requestQueue.add(request);
}