Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading a JSON file containing JSON within brackets

I'm trying to load a JSON file on a website using C# and JSON.Net

However, I'm running into a problem when it runs because all the JSON is within [].

Here's the JSON:

[{"embed_count":"16","name":"live_user_catreina","stream_count":"133","category":"gaming","format":"live","channel_count":272,"title":"SWTOR - Sith Marauder - L42 - Belsavis - The Fatman","featured":true,"site_count":"117","abuse_reported":false,"channel":{"image_url_large":"http://static-cdn.jtvnw.net/jtv_user_pictures/catreina-profile_image-2c63d1c5b60987da-300x300.jpeg","channel_url":"http://www.justin.tv/catreina","category_title":"Gaming","screen_cap_url_large":"http://static-cdn.jtvnw.net/previews/live_user_catreina-320x240.jpg","mature":null,"subcategory":null,"category":"gaming","image_url_medium":"http://static-cdn.jtvnw.net/jtv_user_pictures/catreina-profile_image-2c63d1c5b60987da-150x150.jpeg","subcategory_title":null,"status":"SWTOR - Sith Marauder - L42 - Belsavis - The Fatman","screen_cap_url_medium":"http://static-cdn.jtvnw.net/previews/live_user_catreina-150x113.jpg","image_url_small":"http://static-cdn.jtvnw.net/jtv_user_pictures/catreina-profile_image-2c63d1c5b60987da-70x70.jpeg","timezone":"US/Eastern","screen_cap_url_small":"http://static-cdn.jtvnw.net/previews/live_user_catreina-70x53.jpg","id":5895485,"views_count":"6142420","embed_enabled":true,"embed_code":"    <object type=\"application/x-shockwave-flash\" height=\"295\" width=\"353\" id=\"live_embed_player_flash\" data=\"http://www.justin.tv/widgets/live_embed_player.swf?channel=catreina\" bgcolor=\"#000000\"><param name=\"allowFullScreen\" value=\"true\" /><param name=\"allowscriptaccess\" value=\"always\" /><param name=\"movie\" value=\"http://www.justin.tv/widgets/live_embed_player.swf\" /><param name=\"flashvars\" value=\"start_volume=25&channel=catreina&auto_play=false\" /></object>\n","producer":true,"image_url_tiny":"http://static-cdn.jtvnw.net/jtv_user_pictures/catreina-profile_image-2c63d1c5b60987da-50x50.jpeg","image_url_huge":"http://static-cdn.jtvnw.net/jtv_user_pictures/catreina-profile_image-2c63d1c5b60987da-600x600.jpeg","language":"en","tags":"games gaming lord lotro mmo mmorpg of online pc rings rpg sc2 scii starcraft starcraft2 the vindictus warcraft wow","login":"catreina","screen_cap_url_huge":"http://static-cdn.jtvnw.net/previews/live_user_catreina-630x473.jpg","title":"Gaming With Catreina"},"video_height":720,"language":"en","video_bitrate":1987.1328125,"id":"2309110144","meta_game":"Star Wars: The Old Republic","broadcaster":"fme","broadcast_part":4,"audio_codec":"uncompressed","up_time":"Mon Dec 26 00:06:03 2011","video_width":1280,"geo":"US","channel_view_count":6133751,"channel_subscription":false,"embed_enabled":true,"stream_type":"live","video_codec":"AVC"}]

I try to load it with this code:

class Program
    {
        static void Main(string[] args)
        {
            WebClient webclient = new WebClient();
            var data = webclient.DownloadString("http://api.justin.tv/api/stream/list.json?channel=catreina");
            JObject jo = JObject.Parse(data);
            Console.WriteLine("Embed Count: " + jo["embed_count"]);
            Console.ReadLine();
        }
    }

But it obviously gives me this error

Unhandled Exception: System.Exception: Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray

How do I load the JSON with the [] and then parse the values?

like image 441
user1104783 Avatar asked Dec 28 '22 08:12

user1104783


1 Answers

When a [] is used in JSON, that means an array.

Use a JArray instead of a JObject.

WebClient webclient = new WebClient();
var data = webclient.DownloadString("http://api.justin.tv/api/stream/list.json?channel=catreina");
JArray ja = JArray.Parse(data);
Console.WriteLine("Embed Count: " + ja[0]["embed_count"]);
Console.ReadLine();
like image 179
Oded Avatar answered Jan 05 '23 05:01

Oded