使用 volley 获取 json 对象和数组 [英] fetching json objects and array using volley

查看:69
本文介绍了使用 volley 获取 json 对象和数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取以下 JSONArray 对象.有 2 个项目,但通过我实现的循环,我只能获取 1 个项目.我使用了 2 个 for 循环.如何解决?有没有其他方法可以获取所有数据?如果我从 -1 开始 for 循环,那么它会显示数组索引越界.

I am trying to fetch the below JSONArray objects. There are 2 items but with the loop I have implemented, I am able to fetch only 1 item. I have used 2 for loops. How to resolve it? Is there any other way of fetching all the data? If I start the for loop from -1 then it shows array index out of bounds.

JSON 结构如下

    {
    "status": 200,
    "list": [
        {
            "quot_uid": "QUOTE2018@1",
            "id": "1",
            "expiry_date": "2018-05-29",
            "created_at": "2018-05-22 11:45:58",
            "left_days": "9",
            "items": [
                {
                    "ITEM_NAME": "Copper Wires",
                    "UNIT": "MT",
                    "qty": "5",
                    "make": null
                },
                {
                    "ITEM_NAME": "OFC Cables",
                    "UNIT": "MT",
                    "qty": "2",
                    "make": null
                }
            ]
        }
    ]
}

这是我已经尝试过的代码

And this is the code which I'm already tried

 JSONObject jsonObject = new JSONObject(response);
                        JSONArray jsonArray = jsonObject.getJSONArray("list");
                        tv.removeAllViewsInLayout();
                        int flag = 1;

                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                            JSONArray jsonArray1 = jsonObject1.getJSONArray("items");
                            for (int j = 0; j < jsonArray1.length(); j++) {
                            TableRow tr = new TableRow(Main2Activity.this);
                            tr.setLayoutParams(new ViewGroup.LayoutParams(
                                    ViewGroup.LayoutParams.FILL_PARENT,
                                    ViewGroup.LayoutParams.WRAP_CONTENT));
                            if (flag == 1) {

                                TextView t1 = new TextView(Main2Activity.this);
                                t1.setPadding(10, 30, 10, 30);
                                t1.setText("ITEM NAME");
                                t1.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
                                t1.setTop(20);
                                t1.setTextColor(Color.BLACK);
                                t1.setTextSize(15);
                                t1.setTypeface(null, Typeface.BOLD);
                                tr.addView(t1);

                                TextView t2 = new TextView(Main2Activity.this);
                                t2.setPadding(10, 30, 10, 30);
                                t2.setText("QTY");
                                t2.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
                                t2.setTop(20);
                                t2.setTextColor(Color.BLACK);
                                t2.setTextSize(15);
                                t2.setTypeface(null, Typeface.BOLD);
                                tr.addView(t2);

                                tv.addView(tr);
                                final View vline = new View(Main2Activity.this);
                                vline.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 10));
                                vline.setBackgroundColor(Color.BLUE);
                                tv.addView(vline);
                                flag = 0;
                            } else {
                                JSONObject jsonObject2 = jsonArray1.getJSONObject(j);
                                TextView tv1 = new TextView(Main2Activity.this);
                                tv1.setPadding(5, 30, 5, 30);
                                String item_nm = jsonObject2.getString("ITEM_NAME");
                                tv1.setText(item_nm);
                                tv1.setTextColor(Color.BLACK);
                                tv1.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
                                tv1.setTextSize(15);
                                tr.addView(tv1);

                                TextView tv2 = new TextView(Main2Activity.this);
                                tv2.setPadding(5, 30, 5, 30);
                                String item_qty = jsonObject2.getString("qty");
                                tv2.setText(item_qty);
                                tv2.setTextColor(Color.BLACK);
                                tv2.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
                                tv2.setTextSize(15);
                                tr.addView(tv2);

                                tv.addView(tr);
                                final View vline1 = new View(Main2Activity.this);
                                vline1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
                                vline1.setBackgroundColor(Color.rgb(1, 132, 143));
                                tv.addView(vline1);
                            }

推荐答案

试试这个对我有用.我只是给出了完整的架构打印或使用您想要的值作为您的要求,对于我记录了几个值的示例.

Try this it works for me. I just gave the complete architecture print or use the value you wanted as your requirement, for a sample I Logged few values.

主要区别是我用过

JSONObject listObj1=new JSONObject(jsonArray.get(i).toString());

代替你的线路

JSONObject listObj1=newjsonArray.getJSONObject(i);

完整的代码框架如下.我在字符串中使用了您的 JSON,而不是从响应中获取,如果有效,请尝试一下,然后将其更改为响应.对我来说效果很好.

The complete code skeleton is below. I used your JSON in a String instead taking from a response, try it if it works then change it to a response. It works well for me.

        String response="{'status':200,'list':[{'quot_uid':'QUOTE2018@1','id':'1','expiry_date':'2018-05-29','created_at':'2018-05-2211:45:58','left_days':'9','items':[{'ITEM_NAME':'CopperWires','UNIT':'MT','qty':'5','make':null},{'ITEM_NAME':'OFCCables','UNIT':'MT','qty':'2','make':null}]}]}";
    JSONObject jsonObject = null;
    try {
        jsonObject = new JSONObject(response);

        String status=jsonObject.get("status").toString();
        Log.e("Status is ",status);
        JSONArray jsonArray = jsonObject.getJSONArray("list");
        for(int i=0;i<jsonArray.length();i++){

            //In this loop, you will parse all the array elements inside list array

            JSONObject listObj1=new JSONObject(jsonArray.get(i).toString());

            String qoutid= listObj1.getString("quot_uid");

            Log.e("quot uid is ",qoutid);

            JSONArray lisItems=listObj1.getJSONArray("items");


            for(int j=0;j<lisItems.length();j++){


                JSONObject innerObj=new JSONObject(lisItems.get(j).toString());

                String ITEM_NAME=innerObj.getString("ITEM_NAME");

                Log.e("TAG",ITEM_NAME);

            }

        }


    } catch (JSONException e) {
        Log.e("TAG","Error "+e.toString());
        e.printStackTrace();
    }

还要检查错误日志,因为它会返回任何 JSON 解析错误

check error log as well because it will return JSON parse error any

这篇关于使用 volley 获取 json 对象和数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆