【问题标题】:Android Studio volley nested jsonarrayAndroid Studio凌空嵌套jsonarray
【发布时间】:2021-12-03 12:03:41
【问题描述】:

我正在尝试获取 json 字符串的标题值 (一旦它作为一个对象开始,一次作为一个数组)

我设法得到了一个结果,但它给了我额外的括号等。

title: {"value":"New Beginnings"}

而不仅仅是

title: New Beginnings

我尝试使用的 2 个网址是

https://jhookcrochet.eu/categories/yarniversity/rest?_format=json

https://jhookcrochet.eu/?_format=json

对此的任何帮助将不胜感激! 我想出的代码是:

RequestQueue rq = Volley.newRequestQueue(getActivity().getApplicationContext());

String url = "https://jhookcrochet.eu/?_format=json";
StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        String json = "";
        //txt.setText(response.toString());
        try {
            new JSONObject(response);
            json = "object";
        } catch (JSONException ex) {
            json = "array";
        }
        if (json == "array") {
            try {
                JSONArray jsonArray = new JSONArray(response);
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jresponse =
                            jsonArray.getJSONObject(i);
                    String title = jresponse.getString("title");
                    //String field_image = jresponse.getString("field_image");
                    String body = jresponse.getString("body");
                    Log.d("title", title);
                    //Log.d("field_image",field_image);
                    Log.d("body", body);
                    textView.append("Title: " + title + "\n");
                }
            } catch (JSONException e) {
                textView.append("NO\n");
                e.printStackTrace();
            }
        }
        else if (json == "object") {
            textView.append("Yep, that is an object\n");
            try {
                JSONObject jsonResponse = new JSONObject(response);
                JSONArray gettitle = jsonResponse.optJSONArray("title");
                for (int j=0;j<gettitle.length(); j++)
                {
                    String title = gettitle.getString(j);
                    Log.d("title", title);
                }
                //textView.append("Title: " + title + "\n");
                //Log.d("title", title);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Log.d("error",error.toString());
    }
});
rq.add(request);

// Inflate the layout for this fragment
return root;

【问题讨论】:

    标签: java json android-studio


    【解决方案1】:

    实际上,我只是想如何显示它。

                    JSONArray jsonArray = jsonResponse.getJSONArray("title");
                    JSONObject titlevalue = jsonArray.getJSONObject(0);
                    String title = titlevalue.getString("value");
    

    这将给出我想要的结果。

    Title: New Beginnings
    

    如果有人也在寻找这个,这是我的剪报。可能是一个更好更好的方法,但这目前对我有用:)

            else if (json == "object") {
                //textView.append("Yep, that is an object\n");
                try {
                    JSONObject jsonResponse = new JSONObject(response);
                    JSONArray jsonArray = jsonResponse.getJSONArray("title");
                    JSONObject titlevalue = jsonArray.getJSONObject(0);
                    String title = titlevalue.getString("value");
                    JSONArray jsonBody = jsonResponse.getJSONArray("body");
                    JSONObject bodyvalue = jsonBody.getJSONObject(0);
                    String body = bodyvalue.getString("value");
                    Log.d("title", title);
                    Log.d("body", body);
                    TitleView.append(title);
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                        textView.setText(Html.fromHtml(body, Html.FROM_HTML_MODE_COMPACT));
                    } else {
                        textView.setText(Html.fromHtml(body));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
    

    【讨论】:

      猜你喜欢
      • 2018-10-27
      • 2017-01-23
      • 2017-10-21
      • 1970-01-01
      • 2017-03-25
      • 2016-12-27
      • 2023-04-08
      • 2015-09-12
      • 1970-01-01
      相关资源
      最近更新 更多