我正在使用的数据的 URL:https ://www.googleapis.com/youtube/v3/videos?part=snippet%2Cstatistics&chart=mostPopular&maxResults=50®ionCode=AU&videoCategoryId=15&key= {YOUR_API_KEY}
我目前正在开发这个可以检测 YouTube 热门话题的 Intelli J Java 命令行应用程序。但是,在编写一些尝试将 YouTube 数据字符串解析为 Video 对象的代码时,我需要帮助。这是我已经完成的一些代码,但不确定它是否适合对 YouTube 数据字符串进行解析:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonReader;
public class YouTubeTrender {
public static void test1() throws FileNotFoundException {
System.out.println("Performing Test 1");
String filename = "data/youtubedata_15_50.json";
int expectedSize = 50;
System.out.println("Testing the file: " + filename);
System.out.println("Expecting size of: " + expectedSize);
// Read data
JsonReader jsonReader = Json.createReader(new FileInputStream(filename));
JsonObject jobj = jsonReader.readObject();
// read the values of the item field
JsonArray items = jobj.getJsonArray("items");
System.out.println("Size of input: " + items.size());
System.out.println("Sucess: " + (expectedSize == items.size()));
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException {
System.out.println("YouTube Trender Application");
test1();
}
}
在我忘记这里的代码之前,我应该提一下,上面导入部分中的 JsonReader、JsonObject、Json.createReader、readObject、JsonArray、getJsonArray、json 都是红色文本作为错误,还有一些希望我创建一个类.