在json对象android中获取数组值

i have a json like this,

{
  "id": 293,
  "type": "post",
  "slug": "a-box-rd",
  "url": "http:\/\/www.godigi.tv\/blog\/2013\/07\/01\/a-box-rd\/",
  "status": "publish",
  "title": "A Box R&D",
  "title_plain": "A Box R&D",
  "content": "",
  "excerpt": "",
  "date": "2013-07-01 09:09:25",
  "modified": "2013-07-01 09:18:09",
  "categories": [
    {
      "id": 15,
      "slug": "info",
      "title": "Info",
      "description": "",
      "parent": 0,
      "post_count": 7
    }
  ],
  "tags": [

  ],
  "author": {
    "id": 2,
    "slug": "eka2013",
    "name": "ekawijaya",
    "first_name": "",
    "last_name": "",
    "nickname": "ekawijaya",
    "url": "",
    "description": ""
  },
  "comments": [

  ],
  "attachments": [
    {
      "id": 298,
      "url": "http:\/\/www.godigi.tv\/wp-content\/uploads\/2013\/07\/rnd.jpg",
      "slug": "rnd",
      "title": "rnd",
      "description": "",
      "caption": "",
      "parent": 293,
      "mime_type": "image\/jpeg",
      "images": {
        "full": {
          "url": "http:\/\/www.godigi.tv\/wp-content\/uploads\/2013\/07\/rnd.jpg",
          "width": 528,
          "height": 493
        },
        "thumbnail": {
          "url": "http:\/\/www.godigi.tv\/wp-content\/uploads\/2013\/07\/rnd-150x150.jpg",
          "width": 150,
          "height": 150
        },
        "medium": {
          "url": "http:\/\/www.godigi.tv\/wp-content\/uploads\/2013\/07\/rnd-300x280.jpg",
          "width": 300,
          "height": 280
        },
        "large": {
          "url": "http:\/\/www.godigi.tv\/wp-content\/uploads\/2013\/07\/rnd.jpg",
          "width": 528,
          "height": 493
        },
        "post-thumbnail": {
          "url": "http:\/\/www.godigi.tv\/wp-content\/uploads\/2013\/07\/rnd-150x150.jpg",
          "width": 150,
          "height": 150
        },
        "custom-small": {
          "url": "http:\/\/www.godigi.tv\/wp-content\/uploads\/2013\/07\/rnd-160x90.jpg",
          "width": 160,
          "height": 90
        },
        "custom-medium": {
          "url": "http:\/\/www.godigi.tv\/wp-content\/uploads\/2013\/07\/rnd-320x180.jpg",
          "width": 320,
          "height": 180
        },
        "custom-large": {
          "url": "http:\/\/www.godigi.tv\/wp-content\/uploads\/2013\/07\/rnd-528x360.jpg",
          "width": 528,
          "height": 360
        },
        "custom-full": {
          "url": "http:\/\/www.godigi.tv\/wp-content\/uploads\/2013\/07\/rnd.jpg",
          "width": 528,
          "height": 493
        }
      }
    }
  ],
  "comment_count": 0,
  "comment_status": "open",
  "custom_fields": {
    "dp_video_poster": [
      "http:\/\/www.godigi.tv\/wp-content\/uploads\/2013\/07\/rnd.jpg"
    ],
    "views": [
      "7"
    ],
    "likes": [
      "0"
    ],
    "dp_video_file": [
      "http:\/\/www.godigi.tv\/wp-content\/uploads\/2013\/07\/03-A-BOX-RD-ada-pak-bulit.mp4"
    ]
  }
},

and i use the code like this =>

jsonarray = jsonobject.getJSONArray("posts");

            for (int i = 0; i < jsonarray.length(); i++) {
                HashMap<String, String> map = new HashMap<String, String>();
                jsonobject = jsonarray.getJSONObject(i);

                JSONObject jsoncustom;
                jsoncustom = jsonobject.getJSONObject("custom_fields");
                JSONArray araycus = jsoncustom.getJSONArray("dp_video_poster");
                String urlvid = araycus.getString(i);


                // Retrive JSON Objects
                map.put("title", jsonobject.getString("title"));
                map.put("date", jsonobject.getString("date"));

                map.put("dp_video_poster", urlvid);
                // Set the JSON Objects into the array
                arraylist.add(map);
            }

what i expect for output is :

title =

date =

poster (this is in folder dp_video_poster) =

video (this is in folder dp_video_file) =

can any body help me with this?

thanks in advance

douhuang5623
douhuang5623 你能给出完整的json链接吗?我不认为这是你给我们的完整json数据
大约 7 年之前 回复
du532861657
du532861657 使用Google的gSon库来解析json,以这种方式解析和获取Java对象会更容易。
大约 7 年之前 回复
douju2599
douju2599 好'{'和'}'表示一个对象,你从第一行获取一个数组。
大约 7 年之前 回复
dongshi3061
dongshi3061 你能提供有效的json吗?使用try{}catch{}来查找问题。
大约 7 年之前 回复
doulianglou0898
doulianglou0898 发生了什么而不是你想要的行为?你得到一个NullPointerException?
大约 7 年之前 回复

1个回答




此函数用于读取您的json文件。
并删除json末尾的冒号。 验证您在本网站上的json http://jsonviewer.stack.hu/ </ p>
</ blockquote>

  public String readFile(String filepath)throws IOException {
File f = new File(filepath);
FileInputStream in = new FileInputStream(f);
int size = in .available();
byte c [] = new byte [size];
for(int i = 0; i&lt; size; i ++){
c [i] =(byte)in.read();

}
String filedata = new String(c,“utf-8”);
return filedata;
}
</ code> </ pre>


此函数将解析您的json文件</ p>
</ blockquote>

  public void parseJson(){
try {
String filepath = Environment.getExternalStorageDirectory()
  • “/j/test.json";
    String data = readFile(filepath);

    JSONObject filedata = new JSONObject(data);
    JSONArray categories =(JSONArray)filedata.get(”categories“); \ n JSONObject categorie =(JSONObject)categories.get(0);

    JSONObject custom_field =(JSONObject)filedata
    .get(“custom_fields”);
    JSONArray dp_video_posters =(JSONArray)custom_field
    .get(“dp_video_poster”);

    JSONArray dp_video_files =(JSONArray)custom_field \ n .get(“dp_video_file”);

    //获取标题
    字符串maintitle =(字符串)filedata.get(“title”);
    //从类别中获取标题
    字符串title =(String) categorie.get(“title”);
    //获取日期
    字符串日期=(字符串)filedata.get(“date”);
    //获取海报
    字符串dp_video_poster =(字符串)dp_video_posters.get( 0);
    //获取视频
    字符串dp_video_file =(字符串)dp_video_files.get(0);

}} catch(JSONException e){
e.printStackTrace();
\ n} catch(IOException e){
e.printStackTrace();
} catch(异常e){
e.printStackTrace();
}

}
</ code> </ pre >
</ div>

展开原文

原文

This function is to read your json file. And remove colon which at the end of your json. Verify your json on this site http://jsonviewer.stack.hu/

public String readFile(String filepath) throws IOException {
    File f = new File(filepath);
    FileInputStream in = new FileInputStream(f);
    int size = in.available();
    byte c[] = new byte[size];
    for (int i = 0; i < size; i++) {
        c[i] = (byte) in.read();
    }
    String filedata = new String(c, "utf-8");
    return filedata;
}

This Function will parse your json file

public void parseJson() {
    try {
        String filepath = Environment.getExternalStorageDirectory()
                + "/j/test.json";
        String data = readFile(filepath);

        JSONObject filedata = new JSONObject(data);
        JSONArray categories = (JSONArray) filedata.get("categories");
        JSONObject categorie = (JSONObject) categories.get(0);
        JSONObject custom_field = (JSONObject) filedata
                .get("custom_fields");
        JSONArray dp_video_posters = (JSONArray) custom_field
                .get("dp_video_poster");

        JSONArray dp_video_files = (JSONArray) custom_field
                .get("dp_video_file");

        // getting title
        String maintitle = (String) filedata.get("title");
        // getting title from categories
        String title = (String) categorie.get("title");
        // getting date
        String date = (String) filedata.get("date");
        // getting poster
        String dp_video_poster = (String) dp_video_posters.get(0);
        // getting video
        String dp_video_file = (String) dp_video_files.get(0);



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

    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐