前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >小案例Josn字符串的生成

小案例Josn字符串的生成

作者头像
张风捷特烈
发布2018-11-09 16:47:09
1.2K0
发布2018-11-09 16:47:09
举报

写了一个安卓的在线播放各个电视台的小demo,放在手机和电视上都能看,但资源写在string.xml里感觉挺low 还是转成json吧,维护起来方便些,或放在服务端也挺好,一个一个修改感觉好枯燥,就用代码吧 其实这个算很简单的,只是借此复习一下流的操作字符串分割以及json的生成json的解析 额外说一点:这里的播放源目前都是可用的哦,使用直播流的软件就能看相应的电视台了

本文任务是把下列data.txt解析成json格式:如
代码语言:javascript
复制
  {
    "name": "CCTV1 综合HD",
    "url": "http://112.50.243.7/PLTV/88888888/224/3221226489/index.m3u8",
    "mdTime": "2018-10-22"
  },

张风捷特烈.png

data.txt
代码语言:javascript
复制
<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依赖
代码语言:javascript
复制
<!-- 为简化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,异常直接抛了)
代码语言:javascript
复制
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
代码语言:javascript
复制
[
  {
    "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解析的实体类
代码语言:javascript
复制
/**
 * 作者:张风捷特烈<br/>
 * 时间:2018/10/22 0022:21:03<br/>
 * 邮箱:1981462002@qq.com<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,解析成实体类集合
代码语言:javascript
复制
 String json = FileHelper.get().readInAssets(this, "data.json");
 List<VideoBean> videoBeans = VideoBean.arrayVideoBeanFromData(json);
3.用listView展示数据:
代码语言:javascript
复制
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());
    }
});

listView.png


后记:捷文规范
1.本文成长记录及勘误表

项目源码

日期

备注

V0.1--无

2018-10-22

小案例Josn字符串的生成

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.10.22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 本文任务是把下列data.txt解析成json格式:如
  • data.txt
  • 一、生成json字符串
    • 0思路步骤:
      • 1.新建一个maven管理的java项目,引入gson依赖
        • 2.文件操作(小demo,异常直接抛了)
          • 3.data.json
          • 二、安卓中的使用
            • 1.json解析的实体类
              • 2.读取data.json,解析成实体类集合
                • 3.用listView展示数据:
                • 后记:捷文规范
                  • 1.本文成长记录及勘误表
                  相关产品与服务
                  云直播
                  云直播(Cloud Streaming Services,CSS)为您提供极速、稳定、专业的云端直播处理服务,根据业务的不同直播场景需求,云直播提供了标准直播、快直播、云导播台三种服务,分别针对大规模实时观看、超低延时直播、便捷云端导播的场景,配合腾讯云视立方·直播 SDK,为您提供一站式的音视频直播解决方案。
                  领券
                  问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档