如何阅读使用数它的名字,与GSON JSON变量 [英] How to read JSON variable that use number for its name, with GSON

查看:144
本文介绍了如何阅读使用数它的名字,与GSON JSON变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我是一个初学者在GSON所以请多多包涵。

我想读这个网址的JSON:

<$c$c>https://gdata.youtube.com/feeds/api/videos?author=radityadika&v=2&alt=jsonc

我成功了念想ID,数据,上传,等等。

一个字符串变量

但是,我怎么能读内容(在项目)?我想要得到的RTSP链接(内容:1 ),但我的Java不断给我的错误,因为我不能名数的变量,如:

 字符串1 =ASD;
 

任何帮助是AP preciated,感谢您的帮助

解决方案

您需要首先定义几个类别:

MyGson

 公共类MyGson {
私人字符串apiVersion;
私有数据的数据;

公开数据的getData(){
    返回的数据;
}
}
 

数据

 公共类数据{
私人字符串更新;
私人诠释totalItems = 0;
私人诠释了startIndex = 0;
私人诠释itemsPerPage = 0;
私人列表&LT;项目&GT;项目;

公开名单&LT;项目&GT; getItems(){
    返回的项目;
}
}
 

项目

 公共类项目{
私人字符串ID;
私人字符串上传;
私人字符串更新;
私人字符串上传;
私人字符串类;
私人字符串名称;
私人字符串描述;
私人地图&LT;整数,字符串&GT;内容;

公共地图&LT;整数,字符串&GT;的getContent(){
    返回的内容;
}
}
 

看看,你的内容是地图,是1,2,3,4,5, 6 ....

您可以定义地图&LT;字符串,字符串&GT;内容但因为所有的按键都是整数。

所以,现在你可以提取任何你想要的值:

启动

  ....
 GSON GSON =新GSON();

    MyGson myGson = gson.fromJson(STR,MyGson.class);

    名单&LT;项目&GT;项目= myGson.getData()getItems()。

    如果(items.size()大于0){
        项目项目= items.get(0);

        字符串myStr的= item.getContent()得到(1)。

        的System.out.println(myStr的);
    }
 

输出:

<$p$p><$c$c>rtsp://r6---sn-cg07lue6.c.youtube.com/CiILENy73wIaGQl1cubZZSUSXxMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp

First, im a beginner in GSON so please bear with me.

I tried to read a JSON from this url :

https://gdata.youtube.com/feeds/api/videos?author=radityadika&v=2&alt=jsonc

I succeed to read a String variable like "id", "data", "uploader", etc.

However, how can i read the content (inside items)? I want to get the RTSP link (content:1) but my Java keep give me error because i cant name a variable with number, like :

String 1 = "asd";

Any help is appreciated, Thanks for your help

解决方案

You need define several classes first of all:

MyGson

public class MyGson {
private String apiVersion;
private Data data;

public Data getData() {
    return data;
}
}

Data

public class Data {
private String updated;
private int totalItems = 0;
private int startIndex = 0;
private int itemsPerPage = 0;
private  List<Item> items;

public List<Item> getItems() {
    return items;
}
}

Item

 public class Item {
private String id;
private String uploaded;
private String updated;
private String uploader;
private String category;
private String title;
private String description;
private Map<Integer, String>  content;

public Map<Integer, String> getContent() {
    return content;
}
}

Take a look, your content is map where key is 1,2,3,4,5,6 ....

You can define Map<String, String> content but since all your keys are integers..

So now you can extract any value you want:

Launcher

 ....
 Gson gson = new Gson();

    MyGson myGson = gson.fromJson(str, MyGson.class);

    List<Item> items = myGson.getData().getItems();

    if(items.size()>0){
        Item item = items.get(0);

        String myStr = item.getContent().get(1);

        System.out.println(myStr);
    }

Output:

rtsp://r6---sn-cg07lue6.c.youtube.com/CiILENy73wIaGQl1cubZZSUSXxMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp

这篇关于如何阅读使用数它的名字,与GSON JSON变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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