加载一个JSON文件,其中包含括号内的JSON

本文关键字:JSON 包含括 文件 一个 加载 | 更新日期: 2023-09-27 18:18:59

我试图在使用c#和JSON的网站上加载JSON文件。净

然而,当它运行时,我遇到了一个问题,因为所有的JSON都在[]内。

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

我试着用下面的代码加载它:

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

但是它显然给了我这个错误

Unhandled Exception: System。异常:从JsonReader读取jobobject时出错。当前JsonReader项不是对象:StartArray

我如何加载JSON与[],然后解析值?

加载一个JSON文件,其中包含括号内的JSON

[]在JSON中使用时,这意味着数组

JArray代替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();