Jobject.Parse returning null

Asked

Viewed 68 times

0

I’m not very suited to Youtube Api V3. But I watched a google Developers live where the google operator shows how to list the activity of a particular channel. Based on that I executed via GET at the following address: https://www.googleapis.com/youtube/v3/activities?part=snippet&channelId=UCaNLpnBnxEeQGfEym8bUr-g&maxResults=1&key={SUA_KEY_DE_DESENVOLVIMENTO}

and he returned this code to me. But this code I’m not getting to do Parse in json on it, because it starts with { ... } and not the pattern that in the case would be nome_do_obj:{ ... }, but being that { ... } no beginning of object definition. I used the code:

NOTE: I used the https://www.nuget.org/packages/Newtonsoft.Json/

to decipher the code:

using using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

public string TentarDecifrar(string textoJson, string key, string value){
JObject jsonData = new JObject(JObject.Parse(textoJson));

return jsonData[key][value];
}

But since the first object has no name/identification it returns NullOperationException when I execute

void Main(){ MessageBox.Show(TentarDecifrar(CONTEUDO_EM_JSON_QUE_O_YOUTUBE_DATA_API_ME_RETORNA, "items", "description"));}

In case when I consulted the Jobject would be

jsonData[""]["items"]["description"] 
/* no caso onde está [""] é onde aparece o objeto que não possui identificação ,ou seja:
{           <= nesta linha
"kind": "youtube#activityListResponse",
 "etag": "\"0KG1mRN7bm3nResDPKHQZpg5-do/HzaV00F-8u901Ma6Odl2HflceQE\"",
 "nextPageToken": "CAEQAA", ... }
*/

I want to get the key items and the value for example description, where the variable CONTEUDO_EM_JSON_QUE_O_YOUTUBE_DATA_API_ME_RETORNA is equal to:

{
 "kind": "youtube#activityListResponse",
 "etag": "\"0KG1mRN7bm3nResDPKHQZpg5-do/HzaV00F-8u901Ma6Odl2HflceQE\"",
 "nextPageToken": "CAEQAA",
 "pageInfo": {
  "totalResults": 6,
  "resultsPerPage": 1
 },
 "items": [
  {
   "kind": "youtube#activity",
   "etag": "\"0KG1mRN7bm3nResDPKHQZpg5-do/yikeFjK_8QolzIG4MyBtbexRdjs\"",
   "id": "VTE0NDU3ODg1NTYxMzk4MjAyMjI2NDc2OTY=",
   "snippet": {
    "publishedAt": "2015-10-25T15:55:56.000Z",
    "channelId": "UCaNLpnBnxEeQGfEym8bUr-g",
    "title": "Minecraft - MiniGames # 1",
    "description": "Todo canal de gamer tem que ter minecraft né ? haha\n\nPag:  www.facebook.com/franporfirio98",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/6AzmgmtHTGY/default.jpg",
      "width": 120,
      "height": 90
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/6AzmgmtHTGY/mqdefault.jpg",
      "width": 320,
      "height": 180
     },
     "high": {
      "url": "https://i.ytimg.com/vi/6AzmgmtHTGY/hqdefault.jpg",
      "width": 480,
      "height": 360
     },
     "standard": {
      "url": "https://i.ytimg.com/vi/6AzmgmtHTGY/sddefault.jpg",
      "width": 640,
      "height": 480
     },
     "maxres": {
      "url": "https://i.ytimg.com/vi/6AzmgmtHTGY/maxresdefault.jpg",
      "width": 1280,
      "height": 720
     }
    },
    "channelTitle": "Fran Gamer",
    "type": "upload"
   }
  }
 ]
}

In short... I wanted to develop an application in Winforms so that I could see the youtube api v3 and it returns in json with the data of the last video sent from that particular channel. Even that I still have doubts between Json and Yaml.

1 answer

1


Try this:

var jsonData = JObject.Parse(textoJson);

var items = (JArray)jsonData["items"];
foreach (var item in items)
{
    var snippet = item["snippet"];
    var description = (JValue)snippet["description"];
    Console.WriteLine(description.Value);
}

Edit: To filter by snippet type: var jsonData = Jobject.Parse(json);

var items = (JArray)jsonData["items"];

foreach (var item in items)
{
    var snippet = item["snippet"];
    if ((string)snippet["type"] != "upload") continue;

    var description = (JValue)snippet["description"];
    Console.WriteLine(description.Value);
}
  • erred in var snipper = item["snippet"]; Cannot Applying with [] to an Expression of type Object`

  • puts the whole code here

  • http://prntscr.com/91p33u

  • ah ta i erred! placed foreach(Object ...) is foreach(var ...)

  • Look at your third line, it’s with Object... switch to var :)

  • I read so fast

  • 1

    That’s it! It happens! :)

  • 1

    thanks so much! because as Youtube Api v2 ended for the platform . NET so I was 0/ desperate for a method to solve the problem. : )

  • is and in case the type is not equal to upload?

  • http://prntscr.com/91p6t3

  • Do you want to do q? filter items where type equals upload only?

  • well yes,.......

  • edited the answer, it’s the simplest way... you could use LINQ too, but I think it’s unnecessary, but if you want to read about it, here it goes http://www.newtonsoft.com/json/help/html/QueryingLINQtoJSON.htm

  • Okay I’ll try this!

  • Pivoan! Can you help me read this json now? http://powdertoy.co.uk/Browse/Comments.json?ID=1681176&Start=0&Count=20 is a comment system generated from a simulator, but I want to put it in xml and I cannot.!

  • ta aí! http://s13.postimg.org/oo4rvhrfb/Capture.png

Show 11 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.