小案例Josn字符串的生成

零、前言

1).写了一个安卓的在线播放各个电视台的小demo,放在手机和电视上都能看,但资源写在string.xml里感觉挺low
2).还是转成json吧,维护起来方便些,或放在服务端也挺好,一个一个修改感觉好枯燥,就用代码吧
3).其实这个算很简单的,只是借此复习一下流的操作字符串分割以及json的生成json的解析
4).额外说一点:这里的播放源目前都是可用的哦,使用直播流的软件就能看相应的电视台了
5).2018-10-23日更新:突然发现Vitamio也能播放本地视频,所以将本地视频的路径也转化成这种json格式

本文任务是把下列data.txt解析成json格式:如
  {
    "name": "CCTV1 综合HD",
    "url": "http://112.50.243.7/PLTV/88888888/224/3221226489/index.m3u8",
    "mdTime": "2018-10-22"
  },
小案例Josn字符串的生成
张风捷特烈.png
data.txt
<item>CCTV1 综合HD★http://112.50.243.7/PLTV/88888888/224/3221226489/index.m3u8</item>
<item>CCTV2 财经HD★http://112.50.243.7/PLTV/88888888/224/3221226554/index.m3u8</item>
<item>CCTV-3 综艺HD★http://183.207.249.5/PLTV/3/224/3221225588/index.m3u8</item>
<item>CCTV4 中文国际★http://112.50.243.7/PLTV/88888888/224/3221226511/index.m3u8</item>
<item>CCTV-5 体育HD★http://183.207.249.15/PLTV/3/224/3221225604/index.m3u8</item>
<item>CCTV-6 电影HD★http://183.207.249.15/PLTV/3/224/3221225548/index.m3u8</item>
<item>CCTV-7 军事与农业HD★http://183.207.249.15/PLTV/3/224/3221225546/index.m3u8</item>
<item>CCTV8 电视剧HD★http://223.110.245.167/ott.js.chinamobile.com/PLTV/3/224/3221227204/index.m3u8</item>
<item>CCTV9 记录HD★http://112.50.243.7/PLTV/88888888/224/3221226566/index.m3u8</item>
<item>CCTV10 科教HD★http://112.50.243.7/PLTV/88888888/224/3221226488/index.m3u8</item>
<item>CCTV11 戏曲HD★http://112.50.243.7/PLTV/88888888/224/3221226493/index.m3u8</item>
<item>CCTV12 HD★http://112.50.243.7/PLTV/88888888/224/3221226623/index.m3u8</item>
<item>CCTV13 新闻HD★http://112.50.243.7/PLTV/88888888/224/3221226507/index.m3u8</item>
<item>CCTV14 少儿HD★http://112.50.243.7/PLTV/88888888/224/3221226497/index.m3u8</item>
<item>CCTV-15 音乐★http://183.207.249.7/PLTV/3/224/3221225568/index.m3u8</item>
<item>安徽卫视HD★http://183.207.249.15/PLTV/3/224/3221225634/index.m3u8</item>
<item>北京卫视HD★http://183.207.249.7/PLTV/3/224/3221225574/index.m3u8</item>
<item>湖南卫视 HD★http://223.110.243.173/PLTV/3/224/3221227220/index.m3u8</item>
<item>浙江卫视 HD★http://223.110.243.173/PLTV/3/224/3221227215/index.m3u8</item>
<item>青海卫视★http://183.207.249.7/PLTV/3/224/3221225580/index.m3u8</item>
<item>山东卫视★http://183.207.249.7/PLTV/3/224/3221225586/index.m3u8</item>
<item>江西卫视★http://183.207.249.15/PLTV/3/224/3221225537/index.m3u8</item>
<item>兵团卫视HD★http://v.btzx.com.cn:1935/live/weishi.stream/playlist.m3u8</item>
<item>东方卫视HD★http://111.48.34.182/huaweicdn.hb.chinamobile.com/PLTV/88888888/224/3221225810/1.m3u8?icpid=88888888&#038;from=1&#038;hms_devid=1143</item>
<item>河北卫视★http://183.207.249.7/PLTV/3/224/3221225602/index.m3u8</item>
<item>*卫视★http://183.207.249.15/PLTV/3/224/3221225523/index.m3u8</item>
<item>四川卫视★http://183.207.249.15/PLTV/3/224/3221225570/index.m3u8</item>
<item>*卫视★http://183.207.249.15/PLTV/3/224/3221225579/index.m3u8</item>
<item>爱情喜剧★http://183.207.249.15/PLTV/3/224/3221225525/index.m3u8</item>
<item>武林外传★http://dlhls.cdn.zhanqi.tv/zqlive/33765_6qakZ.m3u8</item>
<item>CNTV古装剧场★http://183.207.249.7/PLTV/3/224/3221225527/index.m3u8</item>
<item>动画王国HD★http://183.207.249.15/PLTV/3/224/3221225555/index.m3u8</item>
<item>海外剧场★http://183.207.249.15/PLTV/3/224/3221225547/index.m3u8</item>
<item>完美游戏★http://183.207.249.15/PLTV/3/224/3221225539/index.m3u8</item>
<item>电影轮播★http://112.50.243.7/PLTV/88888888/224/3221226670/index.m3u8</item>
<item>金鹰纪实HD★http://112.50.243.7/PLTV/88888888/224/3221226613/index.m3u8</item>

一、生成json字符串

0思路步骤:

1).新建一个maven管理的java项目,引入gson依赖
2).用文件读流+bufferd包装类一行一行读取data.txt,对每行的字符串切割
3).建一个实体类,每读一行将数据加载如实体类
4).将实体类集合输出成Json格式字符串,并通过文件写流+bufferd写出

1.新建一个maven管理的java项目,引入gson依赖
<!-- 为简化set,get,toString代码引入lombok -->
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.2</version>
    <scope>provided</scope>
</dependency>

<!--gson依赖-->
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.5</version>
</dependency>
2.文件操作(小demo,异常直接抛了)
public class OnlineVideo {
    public static void main(String[] args) throws IOException {
        //读取文件
        String path = "I:\\Java\\SpringBoot\\small_project\\src\\main\\resources\\data.txt";
        BufferedReader br = new BufferedReader(new FileReader(path));
        ArrayList<VideoBean> videoBeans = new ArrayList<VideoBean>();

        String line = "";
        while ((line = br.readLine()) != null) {
            String res = line.split(">")[1].split("<")[0];
            VideoBean videoBean = new VideoBean();
            videoBean.setName(res.split("★")[0]);
            videoBean.setUrl(res.split("★")[1]);
            videoBean.setMdTime(new SimpleDateFormat("YYYY-MM-dd").format(System.currentTimeMillis()));
            videoBeans.add(videoBean);
        }
        br.close();
        String json = new GsonBuilder().create().toJson(videoBeans);

        String target = "I:\\Java\\SpringBoot\\small_project\\src\\main\\resources\\data.json";
        FileWriter fileWriter = new FileWriter(target);
        fileWriter.write(json);
        fileWriter.close();
    }
}
3.data.json
[
  {
    "name": "CCTV1 综合HD",
    "url": "http://112.50.243.7/PLTV/88888888/224/3221226489/index.m3u8",
    "mdTime": "2018-10-22"
  },
  {
    "name": "CCTV2 财经HD",
    "url": "http://112.50.243.7/PLTV/88888888/224/3221226554/index.m3u8",
    "mdTime": "2018-10-22"
  },
  {
    "name": "CCTV-3 综艺HD",
    "url": "http://183.207.249.5/PLTV/3/224/3221225588/index.m3u8",
    "mdTime": "2018-10-22"
  },
  {
    "name": "CCTV4 中文国际",
    "url": "http://112.50.243.7/PLTV/88888888/224/3221226511/index.m3u8",
    "mdTime": "2018-10-22"
  },
  {
    "name": "CCTV-5 体育HD",
    "url": "http://183.207.249.15/PLTV/3/224/3221225604/index.m3u8",
    "mdTime": "2018-10-22"
  },
  {
    "name": "CCTV-6 电影HD",
    "url": "http://183.207.249.15/PLTV/3/224/3221225548/index.m3u8",
    "mdTime": "2018-10-22"
  },
  {
    "name": "CCTV-7 军事与农业HD",
    "url": "http://183.207.249.15/PLTV/3/224/3221225546/index.m3u8",
    "mdTime": "2018-10-22"
  },
  {
    "name": "CCTV8 电视剧HD",
    "url": "http://223.110.245.167/ott.js.chinamobile.com/PLTV/3/224/3221227204/index.m3u8",
    "mdTime": "2018-10-22"
  },
  {
    "name": "CCTV9 记录HD",
    "url": "http://112.50.243.7/PLTV/88888888/224/3221226566/index.m3u8",
    "mdTime": "2018-10-22"
  },
  {
    "name": "CCTV10 科教HD",
    "url": "http://112.50.243.7/PLTV/88888888/224/3221226488/index.m3u8",
    "mdTime": "2018-10-22"
  },
  {
    "name": "CCTV11 戏曲HD",
    "url": "http://112.50.243.7/PLTV/88888888/224/3221226493/index.m3u8",
    "mdTime": "2018-10-22"
  },
  {
    "name": "CCTV12 HD",
    "url": "http://112.50.243.7/PLTV/88888888/224/3221226623/index.m3u8",
    "mdTime": "2018-10-22"
  },
  {
    "name": "CCTV13 新闻HD",
    "url": "http://112.50.243.7/PLTV/88888888/224/3221226507/index.m3u8",
    "mdTime": "2018-10-22"
  },
  {
    "name": "CCTV14 少儿HD",
    "url": "http://112.50.243.7/PLTV/88888888/224/3221226497/index.m3u8",
    "mdTime": "2018-10-22"
  },
  {
    "name": "CCTV-15 音乐",
    "url": "http://183.207.249.7/PLTV/3/224/3221225568/index.m3u8",
    "mdTime": "2018-10-22"
  },
  {
    "name": "安徽卫视HD",
    "url": "http://183.207.249.15/PLTV/3/224/3221225634/index.m3u8",
    "mdTime": "2018-10-22"
  },
  {
    "name": "北京卫视HD",
    "url": "http://183.207.249.7/PLTV/3/224/3221225574/index.m3u8",
    "mdTime": "2018-10-22"
  },
  {
    "name": "湖南卫视 HD",
    "url": "http://223.110.243.173/PLTV/3/224/3221227220/index.m3u8",
    "mdTime": "2018-10-22"
  },
  {
    "name": "浙江卫视 HD",
    "url": "http://223.110.243.173/PLTV/3/224/3221227215/index.m3u8",
    "mdTime": "2018-10-22"
  },
  {
    "name": "青海卫视",
    "url": "http://183.207.249.7/PLTV/3/224/3221225580/index.m3u8",
    "mdTime": "2018-10-22"
  },
  {
    "name": "山东卫视",
    "url": "http://183.207.249.7/PLTV/3/224/3221225586/index.m3u8",
    "mdTime": "2018-10-22"
  },
  {
    "name": "江西卫视",
    "url": "http://183.207.249.15/PLTV/3/224/3221225537/index.m3u8",
    "mdTime": "2018-10-22"
  },
  {
    "name": "兵团卫视HD",
    "url": "http://v.btzx.com.cn:1935/live/weishi.stream/playlist.m3u8",
    "mdTime": "2018-10-22"
  },
  {
    "name": "东方卫视HD",
    "url": "http://111.48.34.182/huaweicdn.hb.chinamobile.com/PLTV/88888888/224/3221225810/1.m3u8?icpid\u003d88888888\u0026#038;from\u003d1\u0026#038;hms_devid\u003d1143",
    "mdTime": "2018-10-22"
  },
  {
    "name": "河北卫视",
    "url": "http://183.207.249.7/PLTV/3/224/3221225602/index.m3u8",
    "mdTime": "2018-10-22"
  },
  {
    "name": "*卫视",
    "url": "http://183.207.249.15/PLTV/3/224/3221225523/index.m3u8",
    "mdTime": "2018-10-22"
  },
  {
    "name": "四川卫视",
    "url": "http://183.207.249.15/PLTV/3/224/3221225570/index.m3u8",
    "mdTime": "2018-10-22"
  },
  {
    "name": "*卫视",
    "url": "http://183.207.249.15/PLTV/3/224/3221225579/index.m3u8",
    "mdTime": "2018-10-22"
  },
  {
    "name": "爱情喜剧",
    "url": "http://183.207.249.15/PLTV/3/224/3221225525/index.m3u8",
    "mdTime": "2018-10-22"
  },
  {
    "name": "武林外传",
    "url": "http://dlhls.cdn.zhanqi.tv/zqlive/33765_6qakZ.m3u8",
    "mdTime": "2018-10-22"
  },
  {
    "name": "CNTV古装剧场",
    "url": "http://183.207.249.7/PLTV/3/224/3221225527/index.m3u8",
    "mdTime": "2018-10-22"
  },
  {
    "name": "动画王国HD",
    "url": "http://183.207.249.15/PLTV/3/224/3221225555/index.m3u8",
    "mdTime": "2018-10-22"
  },
  {
    "name": "海外剧场",
    "url": "http://183.207.249.15/PLTV/3/224/3221225547/index.m3u8",
    "mdTime": "2018-10-22"
  },
  {
    "name": "完美游戏",
    "url": "http://183.207.249.15/PLTV/3/224/3221225539/index.m3u8",
    "mdTime": "2018-10-22"
  },
  {
    "name": "电影轮播",
    "url": "http://112.50.243.7/PLTV/88888888/224/3221226670/index.m3u8",
    "mdTime": "2018-10-22"
  },
  {
    "name": "金鹰纪实HD",
    "url": "http://112.50.243.7/PLTV/88888888/224/3221226613/index.m3u8",
    "mdTime": "2018-10-22"
  }
]

二、安卓中的使用

可以放在服务器上,用网络访问,这里就放在assets目录下测试
添加依赖implementation 'com.google.code.gson:gson:2.2.4'

1.json解析的实体类
/**
 * 作者:张风捷特烈<br/>
 * 时间:2018/10/22 0022:21:03<br/>
 * 邮箱:[email protected]<br/>
 * 说明:json解析的实体类
 */
public class VideoBean {

 /**
     * name : CCTV1 综合HD
     * url : http://112.50.243.7/PLTV/88888888/224/3221226489/index.m3u8
     * mdTime : 2018-10-22
     */

    private String name;
    private String url;
    private String mdTime;

    public static VideoBean objectFromData(String str) {

        return new Gson().fromJson(str, VideoBean.class);
    }

    public static List<VideoBean> arrayVideoBeanFromData(String str) {

        Type listType = new TypeToken<ArrayList<VideoBean>>() {
        }.getType();

        return new Gson().fromJson(str, listType);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getMdTime() {
        return mdTime;
    }

    public void setMdTime(String mdTime) {
        this.mdTime = mdTime;
    }
2.读取data.json,解析成实体类集合
 String json = FileHelper.get().readInAssets(this, "data.json");
 List<VideoBean> videoBeans = VideoBean.arrayVideoBeanFromData(json);
3.用listView展示数据:
mIdLvVideo.setAdapter(new MyLVAdapter<VideoBean>(this, videoBeans, R.layout.item_text_only) {
    @Override
    public void setData(MyLVHolder holder, VideoBean data, int position) {
        holder.setText(R.id.id_tv_name, data.getName());
    }
});
小案例Josn字符串的生成
listView.png

三、安卓本地视屏路径的获取即json格式转化(权限自理)

1.一个json对象:
  {
    "dataAdd": "1539940191",
    "duration": 985107,
    "mdTime": "1539940191",
    "mime": "video/mp4",
    "name": "16_fcntl",
    "size": 32926815,
    "url": "/storage/emulated/0/linux服务器开发一-基础视频/linux服务器开发一-基础视频5/16_fcntl.mp4"
  },
2.实体类:VideoBean字段
 private String name;//名称
 private String url;//路径
 private String mdTime;//修改时间
 private Long duration;//时常
 private Long size;//大小
 private String mime;//类型
 private String dataAdd;//添加的时间
3.操作媒体的内容提供者,获取视频信息,并转化为Json

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pic100);
        ButterKnife.bind(this);
        mList = findVideo();
        String json = new GsonBuilder().create().toJson(mList);
        FileHelper.get().writeFile2SD("VideoData.json", json);
    }

private ArrayList<VideoBean> findVideo() {
    ArrayList<VideoBean> videoBeans = new ArrayList<>();
    //1.查询获得游标
    Cursor cursor = getContentResolver().query(
            MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, "",
            null, "date_added desc", null);
    //2.让游标移动,到尾为止
    while (cursor.moveToNext()) {
        //获取路径
        String filePath = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DATA));
        //获取时长
        String duration = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DURATION));
        //获取大小
        String size = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.SIZE));
        //获取mimeType
        String mime = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.MIME_TYPE));
        //添加时间
        String dataAdd = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DATE_ADDED));
        //修改时间
        String dataMod = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DATE_MODIFIED));
        //获取名称
        String name = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.TITLE));
        VideoBean videoBean = new VideoBean(name, filePath,
                Long.valueOf(duration), Long.valueOf(size), mime, dataAdd, dataMod);
        videoBeans.add(videoBean);
    }
    return videoBeans;
}
小案例Josn字符串的生成
安卓视频操作.png

后记:捷文规范

1.本文成长记录及勘误表
项目源码 日期 备注
V0.1--无 2018-10-22 小案例Josn字符串的生成
V0.2--无 2018-10-23 添加本地视频路径转化为json格式的方法
2.更多关于我
笔名 QQ 微信 爱好
张风捷特烈 1981462002 zdl1994328 语言
我的github 我的简书 我的CSDN 个人网站
3.声明

1----本文由张风捷特烈原创,转载请注明
2----欢迎广大编程爱好者共同交流
3----个人能力有限,如有不正之处欢迎大家批评指证,必定虚心改正
4----看到这里,我在此感谢你的喜欢与支持