得到JSON对象的Andr​​oid在数组值 [英] get value in array in json object android

查看:138
本文介绍了得到JSON对象的Andr​​oid在数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的JSON,

  {
  ID:293,
  类型:后,
  塞:一箱-RD,
  URL:HTTP:\ / \ / www.godigi.tv \ /博客\ / 2013 \ / 07 \ / 01 \ /一箱-RD \ /,
  状态:发布,
  头衔:一箱R&安培;#038; D,
  title_plain:一箱R&安培;#038; D,
  内容: ,
  摘抄: ,
  日期:2013年7月1日9时09分25秒,
  修改:2013年7月1日九时18分09秒,
  类别:
    {
      ID:15,
      塞:信息,
      头衔:信息,
      描述: ,
      父:0,
      post_count:7
    }
  ]
  标签:

  ]
  作者: {
    ID:2,
    塞:eka2013
    名:ekawijaya
    名字: ,
    姓: ,
    昵称:ekawijaya
    URL:,
    描述: 
  },
  意见:

  ]
  附件:
    {
      ID:298,
      URL:HTTP:\ / \ / www.godigi.tv \ /可湿性粉剂内容\ /上传\ / 2013 \ / 07 \ /rnd.jpg
      塞:RND,
      头衔:RND,
      描述: ,
      标题: ,
      父:293,
      MIME_TYPE:影像\ / JPEG,
      形象:{
        充分: {
          URL:HTTP:\ / \ / www.godigi.tv \ /可湿性粉剂内容\ /上传\ / 2013 \ / 07 \ /rnd.jpg
          宽:528,
          高度:493
        },
        缩略图:{
          URL:HTTP:\ / \ / www.godigi.tv \ /可湿性粉剂内容\ /上传\ / 2013 \ / 07 \ /rnd-150x150.jpg
          宽:150,
          高度:150
        },
        中: {
          URL:HTTP:\ / \ / www.godigi.tv \ /可湿性粉剂内容\ /上传\ / 2013 \ / 07 \ /rnd-300x280.jpg
          宽:300,
          高度:280
        },
        大: {
          URL:HTTP:\ / \ / www.godigi.tv \ /可湿性粉剂内容\ /上传\ / 2013 \ / 07 \ /rnd.jpg
          宽:528,
          高度:493
        },
        后缩略图:{
          URL:HTTP:\ / \ / www.godigi.tv \ /可湿性粉剂内容\ /上传\ / 2013 \ / 07 \ /rnd-150x150.jpg
          宽:150,
          高度:150
        },
        定制小:{
          URL:HTTP:\ / \ / www.godigi.tv \ /可湿性粉剂内容\ /上传\ / 2013 \ / 07 \ /rnd-160x90.jpg
          宽:160,
          高度:90
        },
        定制媒体:{
          URL:HTTP:\ / \ / www.godigi.tv \ /可湿性粉剂内容\ /上传\ / 2013 \ / 07 \ /rnd-320x180.jpg
          宽:320,
          高度:180
        },
        定制大:{
          URL:HTTP:\ / \ / www.godigi.tv \ /可湿性粉剂内容\ /上传\ / 2013 \ / 07 \ /rnd-528x360.jpg
          宽:528,
          高度:360
        },
        定制全:{
          URL:HTTP:\ / \ / www.godigi.tv \ /可湿性粉剂内容\ /上传\ / 2013 \ / 07 \ /rnd.jpg
          宽:528,
          高度:493
        }
      }
    }
  ]
  comment_count:0,
  comment_status:打开,
  custom_fields:{
    dp_video_poster:[
      HTTP:\ / \ / www.godigi.tv \ /可湿性粉剂内容\ /上传\ / 2013 \ / 07 \ /rnd.jpg
    ]
    意见:
      7
    ]
    喜欢:[
      0
    ]
    dp_video_file:[
      HTTP:\ / \ / www.godigi.tv \ /可湿性粉剂内容\ /上传\ / 2013 \ / 07 \ /03-A-BOX-RD-ada-pak-bulit.mp4
    ]
  }
},
 

和我用的是code这样=>

  jsonarray = jsonobject.getJSONArray(上岗);

            的for(int i = 0; I< jsonarray.length();我++){
                HashMap的<字符串,字符串>图=新的HashMap<字符串,字符串>();
                的JSONObject = jsonarray.getJSONObject(ⅰ);

                的JSONObject jsoncustom;
                jsoncustom = jsonobject.getJSONObject(custom_fields);
                JSONArray araycus = jsoncustom.getJSONArray(dp_video_poster);
                串urlvid = araycus.getString(ⅰ);


                // Retrive JSON对象
                map.put(标题,jsonobject.getString(标题));
                map.put(日期,jsonobject.getString(日));

                map.put(dp_video_poster,urlvid);
                //设置JSON对象到数组
                arraylist.add(图)
            }
 

我所期望的输出是:

标题=

日期=

海报(这是文件夹dp_video_poster)=

视频(这是文件夹dp_video_file)=

任何机构可以帮助我?

在此先感谢

解决方案
  

这功能是读取你的JSON文件。   并取出结肠这在你的JSON的结束。验证您的JSON在本网站 http://jsonviewer.stack.hu/

 公共字符串READFILE(字符串文件路径)抛出IOException异常{
    文件F =新的文件(文件路径);
    在的FileInputStream =新的FileInputStream(F);
    INT大小= in.available();
    字节C [] =新的字节[尺寸]
    的for(int i = 0; I<大小;我++){
        C [i] =(字节)in.read();
    }
    字符串Filedata上=新的String(C,UTF-8);
    返回Filedata上;
}
 

  

此功能会分析你的JSON文件

 公共无效parseJson(){
    尝试 {
        字符串的文件路径= Environment.getExternalStorageDirectory()
                +/j/test.json;
        字符串数据= READFILE(文件路径);

        JSONObject的Filedata上=新的JSONObject(数据);
        JSONArray类=(JSONArray),其中filedata.get(类);
        JSONObject的categorie =(的JSONObject)categories.get(0);
        JSONObject的custom_field =(的JSONObject)Filedata上
                获得(custom_fields);
        JSONArray dp_video_posters =(JSONArray),其中custom_field
                获得(dp_video_poster);

        JSONArray dp_video_files =(JSONArray),其中custom_field
                获得(dp_video_file);

        //获得冠军
        字符串maintitle =(字符串)filedata.get(标题);
        //获得冠军从分类
        字符串title =(字符串)categorie.get(标题);
        //获取日期
        字符串日期=(字符串)filedata.get(日);
        //获取海报
        串dp_video_poster =(串)dp_video_posters.get(0);
        //获取视频
        串dp_video_file =(串)dp_video_files.get(0);



    }赶上(JSONException E){
        e.printStackTrace();

    }赶上(IOException异常E){
        e.printStackTrace();
    }赶上(例外五){
        e.printStackTrace();
    }

}
 

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

解决方案

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();
    }

}

这篇关于得到JSON对象的Andr​​oid在数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆