【问题标题】:How can I get the JSON values in the TextView?如何在 TextView 中获取 JSON 值?
【发布时间】:2019-11-25 07:00:12
【问题描述】:

这是我用来设置文本的布局:Image Here, please Click

这是我正在使用的数组列表:

 myTeacherModelLists.add(new MyTeacherModel(R.drawable.home_google_classroom, "Classroom","sds"));
        myTeacherModelLists.add(new MyTeacherModel(R.drawable.home_news_alerts, "Notice","sds"));
        myTeacherModelLists.add(new MyTeacherModel(R.drawable.home_school_rank, "Announcement","sds"));
        myTeacherModelLists.add(new MyTeacherModel(R.drawable.school_diary, "School Diary","sds"));
        myTeacherModelLists.add(new MyTeacherModel(R.drawable.home_absent_report, "Absent report","sds"));
        myTeacherModelLists.add(new MyTeacherModel(R.drawable.examination, "Examination","sds"));
        myTeacherModelLists.add(new MyTeacherModel(R.drawable.home_events, "Events","sds"));
        myTeacherModelLists.add(new MyTeacherModel(R.drawable.edu_forum_finals, "Edu Forum","sds"));
        myTeacherModelLists.add(new MyTeacherModel(R.drawable.home_resources, "Edu Bank","sds"));
        myTeacherModelLists.add(new MyTeacherModel(R.drawable.home_help, "Help","sds"));

这是我的 JSON 响应:

{
        "Badges": {
            "Notice": 0,
            "Event": 0
        },
        "Response": {
            "ResponseVal": 1,
            "Reason": "Success! Record Found."
        }
    }

这是 StringRequest 代码,我使用的是 Volley:

 StringRequest stringRequest = new StringRequest(Request.Method.GET, URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.e("URL : ", URL);

            try {
                JSONObject jsonObject = new JSONObject("Badges");
                Log.e("Response is : ", String.valueOf(jsonObject));
            } catch (JSONException e) {
                e.printStackTrace();
            }


        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);

这是我的模型类:

public class MyTeacherModel {

Integer images;
String text;
String Badges;

public MyTeacherModel(){

}

public MyTeacherModel(Integer images, String text, String badges) {
    this.images = images;
    this.text = text;
    Badges = badges;
}

public Integer getImages() {
    return images;
}

public String getText() {
    return text;
}

public String getBadges() {
    return Badges;
}

目前我使用的是硬编码文本,如何从这个 JSON 中获取数据到 TextViews?

【问题讨论】:

  • 你想知道如何解析JSON吗?
  • 我只想知道如何从 json 响应中获取值并将其设置在 TextView 中?
  • 你需要解析JSON
  • 您可以(实际上必须)先使用搜索。

标签: android json


【解决方案1】:

请试试这个代码

StringRequest stringRequest = new StringRequest(Request.Method.GET, URL, new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {

        try {
            JSONObject jsonObject = new JSONObject(response);

            JSONObject jsonObjectBadges = jsonObject.getJSONObject("Badges");
            int Notice = jsonObjectBadges.getInt("Notice");
            int Event = jsonObjectBadges.getInt("Event");

            JSONObject jsonObjectResponse = jsonObject.getJSONObject("Response");
            int ResponseVal = jsonObjectResponse.getInt("ResponseVal");
            String Reason = jsonObjectResponse.getString("Reason");

            Log.e("URL_DATA : ", "Notice : " + Notice  + "\n" + 
            "Event : " + Event  + "\n" + 
            "ResponseVal : " + ResponseVal  + "\n" +
            "Reason : " + Reason  + "\n");

        } catch (JSONException e) {
            e.printStackTrace();
        }


    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {

    }
});

RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);

【讨论】:

    【解决方案2】:

    您需要使用 java json 对象将 json 文本序列化和反序列化为 java 模型。 为了获得更好的体验,请使用 Gson 、 Moshi 、 Jackson 等库

    try {
                JSONObject jsonObject = new JSONObject("Badges");
                Log.e("Response is : ", String.valueOf(jsonObject));
            } catch (JSONException e) {
                e.printStackTrace();
            }
    

    试试这个:

    try {
                JSONObject jsonObject = new JSONObject(response);
                Log.e("Response is : ", String.valueOf(jsonObject));
            } catch (JSONException e) {
                e.printStackTrace();
            }
    

    该响应是您的 json 字符串并且可以初始化。

    【讨论】:

      【解决方案3】:

      您需要通过以下方式根据您的 json 响应创建模型类:

      如果您的 JSON 响应:

      {
          "Badges": {
               "Notice": 0,
               "Event": 0
           },
          "Response": {
               "ResponseVal": 1,
               "Reason": "Success! Record Found."
           }
      }
      

      那么你的模型类应该是(你需要创建三个模型类):

      class NetworkResponse {
         Badges Badges;  // Note: name of your variable should be same as the field received in 
                         // JSON response
         Response Response;
      }
      
      class Badges {
        int Notice;
        int Event;
      }
      
      class Response {
        int ResponseVal;
        String Reason;
      }
      

      现在在 volley 生成的字符串请求代码中,您需要使用 GSON 库按以下方式解析 JSON 响应。

      StringRequest stringRequest = new StringRequest(Request.Method.GET, URL, new Response.Listener<String>() {
              @Override
              public void onResponse(String response) {
                  Log.e("URL : ", URL);
      
                  try {
                      Gson gson = new Gson();
                      NetworkResponse responseObject = gson.fromJson(response,NetworkResponse.class)
      
                  } catch (JSONException e) {
                      e.printStackTrace();
                  }
      
      
              }
          }, new Response.ErrorListener() {
              @Override
              public void onErrorResponse(VolleyError error) {
      
              }
          });
      
          RequestQueue requestQueue = Volley.newRequestQueue(this);
          requestQueue.add(stringRequest);
      

      您可以从 responseObject 访问您的值并通过以下方式将它们填充到您的文本视图中:

      textView.setText(responseObject.Badges.Notice)
      textView.setText(responseObject.Badges.Event)
      
      textView.setText(responseObject.Response.ResponseVal)
      textView.setText(responseObject.Response.Reason)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-11-24
        • 2019-04-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-02
        • 1970-01-01
        相关资源
        最近更新 更多