Java 如何从以下JSON数组中仅获取视图计数?
以下是youtube数据分析视频的JSON响应:Java 如何从以下JSON数组中仅获取视图计数?,java,json,Java,Json,以下是youtube数据分析视频的JSON响应: { "kind": "youtube#videoListResponse", "etag": "\"q5k97EMVGxODeKcDgp8gnMu79wM/nKD2hgTXHwAf_Y8YHghPkBlZJcs\"", "pageInfo": { "totalResults": 1, "resultsPerPage": 1 }, "items": [ { "kind": "youtube#video", "eta
{
"kind": "youtube#videoListResponse",
"etag": "\"q5k97EMVGxODeKcDgp8gnMu79wM/nKD2hgTXHwAf_Y8YHghPkBlZJcs\"",
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 1
},
"items": [
{
"kind": "youtube#video",
"etag": "\"q5k97EMVGxODeKcDgp8gnMu79wM/GLcJdYlEeecN5amO819w0A1mSqU\"",
"id": "PK_HFb8tkUs",
"snippet": {
"publishedAt": "2016-03-14T01:52:34.000Z",
"channelId": "UCHqC-yWZ1kri4YzwRSt6RGQ",
"title": "Full Speech: Donald Trump HUGE Rally in Boca Raton, FL (3-13-16)",
"description": "Sunday, March 13, 2016: GOP Presidential candidate Donald Trump held a campaign rally in Boca Raton, FL at Sunset Cove Amphitheater and spoke to a massive crowd of tens of thousands of supporters.\n\nFull Speech: Donald Trump Rally in Boca Raton, FL (3-13-16)",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/PK_HFb8tkUs/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/PK_HFb8tkUs/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/PK_HFb8tkUs/hqdefault.jpg",
"width": 480,
"height": 360
},
"standard": {
"url": "https://i.ytimg.com/vi/PK_HFb8tkUs/sddefault.jpg",
"width": 640,
"height": 480
}
},
"channelTitle": "Right Side Broadcasting",
"tags": [
"Donald Trump",
"Live Stream",
"Boca Raton",
"Florida",
"Rally",
"Speech",
"Politics",
"Republican Party"
],
"categoryId": "25",
"liveBroadcastContent": "none",
"localized": {
"title": "Full Speech: Donald Trump HUGE Rally in Boca Raton, FL (3-13-16)",
"description": "Sunday, March 13, 2016: GOP Presidential candidate Donald Trump held a campaign rally in Boca Raton, FL at Sunset Cove Amphitheater and spoke to a massive crowd of tens of thousands of supporters.\n\nFull Speech: Donald Trump Rally in Boca Raton, FL (3-13-16)"
},
"defaultAudioLanguage": "en"
},
"contentDetails": {
"duration": "PT2H41M2S",
"dimension": "2d",
"definition": "hd",
"caption": "false",
"licensedContent": true
},
"status": {
"uploadStatus": "processed",
"privacyStatus": "public",
"license": "youtube",
"embeddable": true,
"publicStatsViewable": true
},
"statistics": {
"viewCount": "371675",
"likeCount": "7592",
"dislikeCount": "586",
"favoriteCount": "0",
"commentCount": "1397"
}
}
]
例如,我只想从上面的响应中提取viewCount。在Java中如何做到这一点?我已经在一个字符串中获得了上面的响应,但是如何只提取那些值,比如viewCount?这是一个很好的例子。您必须遍历JSON数组才能获得viewCount字段。我无法给出确切的代码供您使用,因为您没有告诉我们您使用的是什么JSON库,但这里有一些伪代码
// If json is the object containing the JSON data
viewCount = json["items"][0]["statistics"]["viewCount"];
从“items”数组中,您可以得到第一个元素,然后是该元素中的“statistics”对象,然后是统计对象中的“viewCount”字段。试试这个-
String jsonString="";
JsonParser jParser= Json.createParser(new ByteArrayInputStream(jsonString.getBytes()));
while(jParser.hasNext()){
if(jParser.next()==Event.KEY_NAME){
if(jParser.getString().equals("viewCount")){
jParser.next();
System.out.println(jParser.getString());
}
}
}
应正确导入库。请参阅-
.可能的副本