簡體   English   中英

如何在Android中獲取凌空JSONArray

[英]How to GET volley JSONArray in Android

我是Volley JSONArray的新手。 我無法在txtResponse設置 setText ,因為我總是在onErrorResponse遇到此錯誤。

這是我的Logcat

...... com.example.simplevolley.simplevolley W / System.err:org.json.JSONException:值[{“ com_branch_id”:7,“ com_branch_comId”:1,“ com_branch_name”:“ JT Temerloh”,“ com_branch_manager“:” Tn Hj Ahmad Bin Hj Mohd Sidin“,” com_branch_addr“:” 58號,惹蘭易卜拉欣(Jln Masjid Abu Bakar)“,” com_branch_region“:” Temerloh“,” com_branch_state“:” PAHANG“,” com_branch_count“ “:” MALAYSIA“,” com_branch_poscode“:” 28000“,” com_branch_email“:” tmlh@juaratravel.com.my“,” com_branch_licence“:null,” com_branch_phoneNum“:” 09-2965625“,” com_branch_fax“:null, “ com_branch_fb”:null,“ com_branch_ig”:null},{“ com_branch_id”:9,“ com_branch_comId”:1,“ com_branch_name”:“ JT Nibong Tebal”,“ com_branch_manager”:“ Pejabat Penang”,“ com_branch_addr”: No. 10,Tingkat Atas,Jln Pekaka 1,Tmn Pekaka,“,” com_branch_region“:” Nibong Tebal“,” com_branch_state“:” PULAU PINANG“,” com_branch_country“:” MALAYSIA“,” com_branch_poscode“:” 14300“, “ com_branch_email”:“ pen@juaratravel.com.my”,“ com_branch_licence”:空,“ com_branch_phoneNum”:“ 05-7171877”,“ com_branch_fa x“:” 057161877“,” com_branch_fb“:null,” com_branch_ig“:null} ......

我想閱讀此JSON行,但是因為它以JSONArray開頭,所以我很困惑。

{
"res": true,
"datas": [
    {
        "com_branch_id": 7,
        "com_branch_comId": 1,
        "com_branch_name": "JT Temerloh",
        "com_branch_manager": "Tn Hj Ahmad Bin Hj Mohd Sidin",
        "com_branch_addr": "No.58, Jalan Ibrahim (Jln Masjid Abu Bakar)",
        "com_branch_region": "Temerloh",
        "com_branch_state": "PAHANG",
        "com_branch_country": "MALAYSIA",
        "com_branch_poscode": "28000",
        "com_branch_email": "tmlh@juaratravel.com.my",
        "com_branch_licence": null,
        "com_branch_phoneNum": "09-2965625",
        "com_branch_fax": null,
        "com_branch_fb": null,
        "com_branch_ig": null
    },
    {
        "com_branch_id": 9,
        "com_branch_comId": 1,
        "com_branch_name": "JT Nibong Tebal",
        "com_branch_manager": "Pejabat Penang",
        "com_branch_addr": "No. 10, Tingkat Atas, Jln Pekaka 1, Tmn Pekaka,",
        "com_branch_region": "Nibong Tebal",
        "com_branch_state": "PULAU PINANG",
        "com_branch_country": "MALAYSIA",
        "com_branch_poscode": "14300",
        "com_branch_email": "pen@juaratravel.com.my",
        "com_branch_licence": null,
        "com_branch_phoneNum": "05-7171877",
        "com_branch_fax": "057161877",
        "com_branch_fb": null,
        "com_branch_ig": null
    },]}

我需要獲取所有數據值。 這是我的代碼

 private void makeJsonArrayRequest() {
    showpDialog();

    JsonArrayRequest req = new JsonArrayRequest(urlJsonArry,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d("res", response.toString());

                    try {


                        jsonResponse = "";
                        for (int i = 0; i < response.length(); i++) {


                            JSONObject branchi = (JSONObject) response.get(i);
                            JSONObject branch = branchi.getJSONObject("datas");


                            Integer com_branch_id = Integer.valueOf(branch.getString("com_branch_id"));
                            Integer com_branch_comId = Integer.valueOf(branch.getString("com_branch_comId"));
                            String com_branch_name = branch.getString("com_branch_name");
                            String com_branch_manager = branch.getString("com_branch_manager");
                            String com_branch_addr = branch.getString("com_branch_addr");
                            String com_branch_region = branch.getString("com_branch_region");
                            String com_branch_state = branch.getString("com_branch_state");
                            String com_branch_country = branch.getString("com_branch_country");
                            String com_branch_poscode = branch.getString("com_branch_poscode");
                            String com_branch_email = branch.getString("com_branch_email");
                            String com_branch_licence = branch.getString("com_branch_licence");
                            String com_branch_phoneNum = branch.getString("com_branch_phoneNum");
                            String com_branch_fax = branch.getString("datas.com_branch_fax");
                            String com_branch_fb = branch.getString("datas.com_branch_fb");
                            String com_branch_ig = branch.getString("datas.com_branch_ig");


                            jsonResponse = "";
                            jsonResponse += "Branch name: " + com_branch_id + "\n\n";
                            jsonResponse += "Branch manager: " + com_branch_comId + "\n\n";
                            jsonResponse += "Branch state: " + com_branch_name + "\n\n";
                            jsonResponse += "Branch state: " + com_branch_manager + "\n\n";
                            jsonResponse += "Branch state: " + com_branch_addr + "\n\n";
                            jsonResponse += "Branch state: " + com_branch_region + "\n\n";
                            jsonResponse += "Branch state: " + com_branch_state + "\n\n";
                            jsonResponse += "Branch state: " + com_branch_country + "\n\n";
                            jsonResponse += "Branch state: " + com_branch_poscode + "\n\n";
                            jsonResponse += "Branch state: " + com_branch_email + "\n\n";
                            jsonResponse += "Branch state: " + com_branch_licence + "\n\n";
                            jsonResponse += "Branch state: " + com_branch_phoneNum + "\n\n";
                            jsonResponse += "Branch state: " + com_branch_fax + "\n\n";
                            jsonResponse += "Branch state: " + com_branch_fb + "\n\n";
                            jsonResponse += "Branch state: " + com_branch_ig + "\n\n";

                        }

                        txtResponse.setText(jsonResponse);

                    } catch (JSONException e) {
                        e.printStackTrace();
                        Toast.makeText(getApplicationContext(),
                                "res" + e.getMessage(),
                                Toast.LENGTH_LONG).show();

                        txtResponse.setText(e.getMessage());
                    }

                    hidepDialog();
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "res" + error.getMessage());
            Toast.makeText(getApplicationContext(),
                    error.getMessage(), Toast.LENGTH_LONG).show();
            Log.d("onErrorResponse", error.getMessage());
            hidepDialog();

        }
    });

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(req);
}

我嘗試遵循其他解決方案,但不幸的是,我失敗了並陷入困境。

嘗試這個..

 JSONObject json = new JSONObject(response);

    JSONArray jsonarray = json.getJSONArray("datas");

    for (int i = 0; i < jsonarray.length(); i++) {
         JSONObject c = jsonarray.getJSONObject(i);

         //here you get by json key
         String id = c.getString("com_branch_id");

    }

1)您的Json不是數組(它以{表示對象json開頭)。 使用JsonObjectRequest。
2)通過迭代,您可以使用jackson2將響應映射到POJO而不是在響應中獲取元素。
3)您可以使用jsonschema2pojo創建一個POJO類

JsonArrayRequest將為您提供JSONArray響應。 但是您的響應不是JSONArray 它的JSONObject和該JSONObject包含JSONArray

為您的案例使用JsonObjectRequest

您可以使用Gson將響應轉換為Java對象。

制作一個與您的響應格式匹配的Java Pojo類。 比將您的響應轉換為java對象。

        public void onResponse(JSONObject response) {

            String message = "";
            if (response != null) {
                message = response.toString();
                Post post = null;
                Gson gson = new Gson();
                post = gson.fromJson(response.toString(), Post.class);

            } else {
                message = "Response is null";

            }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM