在括号内加载包含 JSON 的 JSON 文件

Posted

技术标签:

【中文标题】在括号内加载包含 JSON 的 JSON 文件【英文标题】:Loading a JSON file containing JSON within brackets 【发布时间】:2011-12-26 16:05:36 【问题描述】:

我正在尝试使用 C# 和 JSON.Net 在网站上加载 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();
        
    

但它显然给了我这个错误

未处理的异常:System.Exception:从 JsonReader 读取 JObject 时出错。当前 JsonReader 项不是对象:StartArray

如何使用 [] 加载 JSON,然后解析值?

【问题讨论】:

还有一个你可能想看的链接,它可以解释使用 C#drowningintechnicaldebt.com/ShawnWeisfeld/archive/2010/08/22/… 以 JSON 格式读取的不同方法 【参考方案1】:

当在 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();

【讨论】:

这很奇怪,我很确定当我试图弄清楚它时我尝试了 JArray 而不是 JObject 但它也没有用,但现在它可以了。一定是之前做错了什么。感谢您的帮助! @user1104783 - 请注意,我使用ja[0] 来访问JArray 中的第一个JObject 最后一个问题,如果数组为空怎么办?我怎样才能发现这个错误?当它为空时,文件只是 [] @user1104783 - ja.Count == 0

以上是关于在括号内加载包含 JSON 的 JSON 文件的主要内容,如果未能解决你的问题,请参考以下文章

如何使用方括号加载 JSON 数据

将带有对象数组的 json 文件从文件加载到 SQL 中

javascript的JSON对象

golang生成JSON及解析JSON

React 可加载 JSON 映射文件,其中包含从多个位置引用的组件

将包含许多嵌套对象的JSON文件保存到列表中