【问题标题】:How to parse json data and load it in the Listview如何解析 json 数据并将其加载到 Listview 中
【发布时间】:2018-01-12 06:36:53
【问题描述】:

我从 api 获取一个 json 对象,我想解析这个 json 对象并获取文本值。我正在使用 volley 库,需要将其加载到 listiew 适配器中。json 是 object-array-object 类型。这是我的 Json 对象。你能帮忙吗?

{"result":[{"id":"1","ExpendName":"rice","Cost":"500","Dates":"2018-01-09 11:13:58"},{"id":"2","ExpendName":"Meat","Cost":"550","Dates":"2018-01-09 11:17:27"},{"id":"3","ExpendName":"Fish","Cost":"250","Dates":"2018-01-09 11:21:30"},{"id":"4","ExpendName":"Pant","Cost":"700","Dates":"2018-01-09 11:36:45"},{"id":"5","ExpendName":"Shirt","Cost":"1000","Dates":"2018-01-09 11:50:11"},{"id":"6","ExpendName":"Tea","Cost":"55","Dates":"2018-01-09 13:37:42"},{"id":"7","ExpendName":"Lunch, Tea, Transport ","Cost":"750","Dates":"2018-01-09 13:41:29"},{"id":"8","ExpendName":"Breakfast ","Cost":"30","Dates":"2018-01-10 05:34:07"},{"id":"9","ExpendName":"Train","Cost":"460","Dates":"2018-01-11 05:32:42"},{"id":"10","ExpendName":"Bus","Cost":"1250","Dates":"2018-01-11 05:33:11"},{"id":"11","ExpendName":"Train","Cost":"500","Dates":"2018-01-11 10:03:00"}]}

【问题讨论】:

  • 好吧,你有没有考虑过在发布这个问题之前进行研究?
  • 我已经搜索并尝试了很多。但在 jsonobject 请求中使用 jsonobject 存在问题。这就是我现在寻求帮助的原因。

标签: android json listview android-volley


【解决方案1】:

您通常会这样解析数据:

try {
    JSONObject jsonObject = new JSONObject(rawData);
    JSONArray jsonArray = jsonObject.getJSONArray("result");
    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject mealObject = jsonArray.getJSONObject(i);
        String id = mealObject.getString("id");
        String expendName = mealObject.getString("ExpendName");
        String cost = mealObject.getString("Cost");
        String date = mealObject.getString("Dates");

        //Do whatever you want with the data
     }
 } catch (JSONException e) {
    e.printStackTrace();
 }

【讨论】:

  • 这里的“rawData”是不是Json对象的“响应”?
  • 这是您在上面发布的 JSON 数据。 rawData 是存储上述 JSON 响应的 String 对象。
  • 查看答案here了解更多关于解析JSON的信息
  • 当我请求json对象请求JSONObject jsonObject = new JSONObject(response);时,这行显示错误JSONObject jsonObject = new JSONObject(response);你能帮忙!!
  • final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener&lt;JSONObject&gt;() {@Overridepublic void onResponse(JSONObject response)
猜你喜欢
  • 1970-01-01
  • 2019-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-05
  • 1970-01-01
  • 1970-01-01
  • 2016-03-03
相关资源
最近更新 更多