简体   繁体   English

如何从json url中的jsonarray获取属性并在textview中显示

[英]How to get the attribute from jsonarray in json url and display in textview

Hi i am trying to get the json response which has attribute with jsonarray 嗨,我正在尝试获取具有jsonarray属性的json响应

like (ex: {A:one,B:[{a:one,b:two},{a:two,b:one}]}) i have trying to get 像(例如:{A:one,B:[{a:one,b:two},{a:two,b:one}]})我已经尝试

the a:one and b:two values only. 仅a:one和b:两个值。 But my logcat says error: 但是我的logcat说错误:

  1. RecyclerView: No adapter attached; RecyclerView:未连接适配器; skipping layout 跳过布局
  2. Json parsing error: Value custom_attributes of type java.lang.String cannot be converted to JSONArray Json解析错误:java.lang.String类型的值custom_attributes无法转换为JSONArray

i want to get this values in textview for my product detail view... 我想在textview中为我的产品详细信息视图获取此值...

My Coding is : 我的编码是:

private class GetProductDetails extends AsyncTask<Void, Void, Void> {



        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog
            dialog_pro = new ProgressDialog(Product_Detail_Activity.this);
            dialog_pro.setMessage("Please wait...");
            dialog_pro.setCancelable(false);
            dialog_pro.show();

        }

        @Override
        protected Void doInBackground(Void... arg0) {
            HttpHandler sh = new HttpHandler();

            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(BaseURL);

            //Log.e(TAG, "Response from url: " + jsonStr);

            if (jsonStr != null) {
                try {
                     JSONObject jsonObj = new JSONObject(jsonStr);

                    name = jsonObj.getString("name");


                    JSONArray items = new JSONArray("custom_attributes");
                    for (int i = 0; i < items.length(); i++) {
                        JSONObject c = items.getJSONObject(i);

                        String atrr = c.getString("attribute_code");

                        if(atrr.equalsIgnoreCase("short_description")) {

                            des = c.getString("value");
                        }

                    }


                }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();
                          //  pro_name.setText("Json error:" + e.getMessage());

                        }
                    });

                }
            } 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();
                    }
                });
                // AppController.getPermission().addToRequestQueue(jsonObj);
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog
            /**
             * Updating parsed JSON data into ListView
             * */
            if (dialog_pro.isShowing())
                dialog_pro.dismiss();
            pro_name.setText(name);
            short_desc.setText(des);
        }

    }

Being based on your JSON 基于您的JSON

try {
            JSONObject responseObject = new JSONObject(response);
            String A= responseObject.getString("A");
            JSONArray bArray= responseObject.getJSONArray("B");
            for(int i=0;i<bArray.length();i++){
                JSONObject innerObject=bArray.getJSONObject(i);
                String a= innerObject.getString("a");
                String b= innerObject.getString("b");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

In your code you have JSONArray items = new JSONArray("custom_attributes"); 在您的代码中,您有JSONArray项目= new JSONArray(“ custom_attributes”); should be changed. 应该改变。 You should get your custom_attributes array from the jsonObj object using object.getJSONArray(). 您应该使用object.getJSONArray()从jsonObj对象获取custom_attributes 数组

For your first problem-RecyclerView: No adapter attached; 对于您的第一个问题-RecyclerView:未连接适配器; skipping layout-> Just set an empty adapter first, update it as soon as you have the data (this is what works for me) For your second problem-Json parsing error: Value custom_attributes of type java.lang.String cannot be converted to JSONArray-> 跳过layout->只需首先设置一个空适配器,一旦有数据就更新它(这对我有用)对于第二个问题-Json解析错误:java.lang.String类型的值custom_attributes无法转换为JSONArray->

The eg json that you have posted "{A:one,B:[{a:one,b:two},{a:two,b:one}]}" it is not in valid json format 您发布的示例json“ {A:one,B:[{a:one,b:two},{a:two,b:one}]}”不是有效的json格式

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

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