如何获得第一个数组值

时间:2017-02-23 12:06:45

标签: android arrays json listview

{
  "status": "true",
  "data": [
    {
      "giid": "91",
      "gid": "26",
      "name": "gallery/image/7475068.png",
      "type": "0",
      "singer": ""
    },
    {
      "giid": "92",
      "gid": "25",
      "name": "gallery/image/8704185.png",
      "type": "0",
      "singer": ""
    },
    {
      "giid": "93",
      "gid": "25",
      "name": "gallery/image/3639377.png",
      "type": "0",
      "singer": ""
    },
    {
      "giid": "94",
      "gid": "25",
      "name": "gallery/image/5589478.png",
      "type": "0",
      "singer": ""
    },
    {
      "giid": "95",
      "gid": "25",
      "name": "gallery/image/3503162.png",
      "type": "0",
      "singer": ""
    }]}

在我的列表视图中只获得第一个值如何可能?

我的代码: -

try {
                        JSONArray jsonObject = response.getJSONArray("data");
                        for (int i = 0; i < jsonObject.length(); i++) {
                            System.out.println("<<<<<<<<<<<<<<<<<<<");
                            JSONObject object1 = jsonObject.getJSONObject(i);

                            Beans_News info = new Beans_News();

                            info.setDate(object1.getString("giid"));
                            info.setNews(object1.getString("name"));
                            info.setNewsid(object1.getString("gid"));
                            info.setStatus(object1.getString("type"));

                            array_bean.add(info);
                            lvls.setAdapter(new CustomAdapter_classwork());
                            pDialog.hide();
                        }
                    } catch (JSONException e) {
                        Toast.makeText(getApplicationContext(), "Not available", Toast.LENGTH_SHORT).show();
                        e.printStackTrace();
                        pDialog.hide();
                    } 

我只希望我的名单中的第一个值请帮助我

5 个答案:

答案 0 :(得分:1)

如果您发送的回复如下:

  res.send({'status':'true', 'data': data})

res.json({'status':'true', 'data': data})

并且这个数据是一个数组然后你可以获得第一个元素,如

obj = response;
let arr= obj.data;
if(arr.length>0) firstElement = arr[0]
else //whatever you want

答案 1 :(得分:0)

JSONObject jsonObject = new JSONObject(response.toString());
JSONArray jArray = jsonObject.getJSONArray("data");
//remove for loop if you only need single data
//for (int i = 0; i < jArray.length(); i++) {
//if you need first value set 0 in index
   JSONObject object1 = jArray.getJSONObject(0);
info.setDate(object1.getString("giid"));
info.setNews(object1.getString("name"));
info.setNewsid(object1.getString("gid"));
info.setStatus(object1.getString("type"));

试试希望它有所帮助

答案 2 :(得分:0)

在此处进行更改

 JSONObject object1 = jsonObject.getJSONObject(0);

使用0索引

答案 3 :(得分:0)

只需设置 .getJSONObject(0);

JSONObject object1 = jsonObject.getJSONObject(0); // Set 0 for First Value
Beans_News info = new Beans_News();
info.setDate(object1.getString("giid"));
info.setNews(object1.getString("name"));
info.setNewsid(object1.getString("gid"));
info.setStatus(object1.getString("type"));
.......

答案 4 :(得分:0)

像这样更新您的代码

JSONObject jsonObject = response.getJSONArray("data").getJSONObject(0);
Beans_News info = new Beans_News();
info.setDate(jsonObject.getString("giid"));
info.setNews(jsonObject.getString("name"));
info.setNewsid(jsonObject.getString("gid"));
info.setStatus(jsonObject.getString("type"));

这是您可以尝试完成任务的最简单的代码。