Как извлечь значения из foreach в виде массива [duplicate]

{
   "pageInfo": {
         "pageName": "abc",
         "pagePic": "http://example.com/content.jpg"
    },
    "posts": [
         {
              "post_id": "123456789012_123456789012",
              "actor_id": "1234567890",
              "picOfPersonWhoPosted": "http://example.com/photo.jpg",
              "nameOfPersonWhoPosted": "Jane Doe",
              "message": "Sounds cool. Can't wait to see it!",
              "likesCount": "2",
              "comments": [],
              "timeOfPost": "1234567890"
         }
    ]
}

Java code :

JSONObject obj = new JSONObject(responsejsonobj);
String pageName = obj.getJSONObject("pageInfo").getString("pageName");

JSONArray arr = obj.getJSONArray("posts");
for (int i = 0; i < arr.length(); i++)
{
    String post_id = arr.getJSONObject(i).getString("post_id");
    ......etc
}
0
задан Lain 4 March 2014 в 00:37
поделиться

2 ответа

Вы можете просто перебирать массив:

for(ContentValues c : params){
    //do something with c
}

, или если вы уверены, что в массиве есть только элемент, вы можете получить к нему доступ напрямую:

ContentValues c = params[0];
3
ответ дан eltabo 27 August 2018 в 11:38
поделиться

, если вы передали массив. Вы можете просто получить его с помощью `ContentValues ​​[] arr = params [0];

1
ответ дан Adnan 27 August 2018 в 11:38
поделиться
Другие вопросы по тегам:

Похожие вопросы: