0

Here is the JSON response of a video for youtube data analytics:

{
 "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"
   }
  }
 ]

I would only like to extract the viewCount for example from the above response. How do I do that in Java? I have obtained the above response in a string but how do I extract just those single values like viewCount? An example is well appreciated.

1

2 Answers 2

0

You would have to traverse the JSON array to get the viewCount field. I can't give you the exact code to use as you have not told us what JSON library you are using, but here's some pseudocode.

// If json is the object containing the JSON data
viewCount = json["items"][0]["statistics"]["viewCount"];

From the "items" array, you get the first element, then the "statistics" object from that element, then the "viewCount" field from the statistics object.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this -

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());
        }
    }
}

The library should be imported correctly. Please refer - https://jsonp.java.net/download.html.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.