如何从android

时间:2017-01-22 21:51:30

标签: java android arrays json

请问我正在尝试从JSON数组中获取URL thumbnail_images,它位于另一个media_large数组中,如何从第二个数组中获取数据。我已经能够从这个JSON获取URL

"attachments": [

{
    "id": 367,
    "url": "http://street2view.com/wp-content/uploads/2017/01/mmm.png",

使用此代码:

JSONArray attachments = post.getJSONArray("attachments");
if (null != attachments && attachments.length() > 0) {
   JSONObject attachment = attachments.getJSONObject(0);
   if (attachment != null)
      item.setAttachmentUrl(attachment.getString("url"));
}

那么如何从内部数组中获取数据,就像在此代码中一样,我如何获取URL

"thumbnail_images": {
    "medium_large": {
        "url": "http://street2view.com/wp-content/uploads/2017/01/mmm.png",
        "width": 749,
        "height": 400

2 个答案:

答案 0 :(得分:3)

这是基于你没有" thumbnail_images"作为一个数组,但它只是一个属性

JSONObject images = post.getJSONObject("thumbnail_images");
JSONObject mediumLarge = images.getJSONObject("medium_large");
String url = mediumLarge.optString("url");

答案 1 :(得分:-1)

我知道了!

<script async=true>

感谢@classicalConditioning提示。