【问题标题】:Parsing array in JSON using ASP.NET C#使用 ASP.NET C# 解析 JSON 中的数组
【发布时间】:2013-08-08 11:51:51
【问题描述】:

我正在使用以下教程来解析 JSON 文档。 http://www.drowningintechnicaldebt.com/ShawnWeisfeld/archive/2010/08/22/using-c-4.0-and-dynamic-to-parse-json.aspx

我试图解析的 JSON 文档可以在这里访问: http://www.visitproject.co.uk/Tweets/Ireland.txt

        JavaScriptSerializer jss = new JavaScriptSerializer();
        jss.RegisterConverters(new JavaScriptConverter[] { new DynamicJsonConverter() });

        dynamic tweets = jss.Deserialize(json, typeof(object)) as dynamic;

        foreach (var tweettext in tweets.statuses.text)
        {
            Console.WriteLine("Tweet: " + tweettext);
        }

我可以在 tweets.statuses 上执行监视,它确实包含一系列推文。我想从每条推文中获取文本值。我可以看到本教程唯一不同的是它是 JSON 中的一个数组,我希望这就是它不起作用的原因。有没有人有任何想法?感谢您的帮助!

【问题讨论】:

  • 你想解析JSON中的所有状态并找出文本吗?这是否假设在服务器端执行?还是客户端?因为我猜客户端会更容易
  • @NisargShah 我将使用 ASP.Net 在服务器端执行此操作。是的,我想解析每个状态,这就是为什么我使用 foreach 但我没有正确设置它并且我得到了一个 RuntimeBinderException。谢谢。

标签: c# asp.net json twitter collections


【解决方案1】:

您可以像这样使用 LINQ to JSON:

// Parse JSON
JObject o = JObject.Parse(json);

阅读LINQ to JSON 文档,了解有关如何查询所需 JSON 片段的详细信息。

【讨论】:

    【解决方案2】:

    您只需复制粘贴下面的代码即可获得答案。 这就是我解析你的 json 数据的方式。

    我根据你的 json 创建了类

    public class Metadata
    {
        public string result_type { get; set; }
        public string iso_language_code { get; set; }
    }
    
    public class Url2
    {
        public string url { get; set; }
        public string expanded_url { get; set; }
        public string display_url { get; set; }
        public List<int> indices { get; set; }
    }
    
    public class Url
    {
        public List<Url2> urls { get; set; }
    }
    
    public class Description
    {
        public List<object> urls { get; set; }
    }
    
    public class Entities
    {
        public Url url { get; set; }
        public Description description { get; set; }
    }
    
    public class User
    {
        public int id { get; set; }
        public string id_str { get; set; }
        public string name { get; set; }
        public string screen_name { get; set; }
        public string location { get; set; }
        public string description { get; set; }
        public string url { get; set; }
        public Entities entities { get; set; }
        public bool @protected { get; set; }
        public int followers_count { get; set; }
        public int friends_count { get; set; }
        public int listed_count { get; set; }
        public string created_at { get; set; }
        public int favourites_count { get; set; }
        public int? utc_offset { get; set; }
        public string time_zone { get; set; }
        public bool geo_enabled { get; set; }
        public bool verified { get; set; }
        public int statuses_count { get; set; }
        public string lang { get; set; }
        public bool contributors_enabled { get; set; }
        public bool is_translator { get; set; }
        public string profile_background_color { get; set; }
        public string profile_background_image_url { get; set; }
        public string profile_background_image_url_https { get; set; }
        public bool profile_background_tile { get; set; }
        public string profile_image_url { get; set; }
        public string profile_image_url_https { get; set; }
        public string profile_link_color { get; set; }
        public string profile_sidebar_border_color { get; set; }
        public string profile_sidebar_fill_color { get; set; }
        public string profile_text_color { get; set; }
        public bool profile_use_background_image { get; set; }
        public bool default_profile { get; set; }
        public bool default_profile_image { get; set; }
        public bool following { get; set; }
        public bool follow_request_sent { get; set; }
        public bool notifications { get; set; }
        public string profile_banner_url { get; set; }
    }
    
    public class Large
    {
        public int w { get; set; }
        public int h { get; set; }
        public string resize { get; set; }
    }
    
    public class Medium2
    {
        public int w { get; set; }
        public int h { get; set; }
        public string resize { get; set; }
    }
    
    public class Thumb
    {
        public int w { get; set; }
        public int h { get; set; }
        public string resize { get; set; }
    }
    
    public class Small
    {
        public int w { get; set; }
        public int h { get; set; }
        public string resize { get; set; }
    }
    
    public class Sizes
    {
        public Large large { get; set; }
        public Medium2 medium { get; set; }
        public Thumb thumb { get; set; }
        public Small small { get; set; }
    }
    
    public class Medium
    {
        public object id { get; set; }
        public string id_str { get; set; }
        public List<int> indices { get; set; }
        public string media_url { get; set; }
        public string media_url_https { get; set; }
        public string url { get; set; }
        public string display_url { get; set; }
        public string expanded_url { get; set; }
        public string type { get; set; }
        public Sizes sizes { get; set; }
        public long source_status_id { get; set; }
        public string source_status_id_str { get; set; }
    }
    
    public class Entities2
    {
        public List<object> hashtags { get; set; }
        public List<object> symbols { get; set; }
        public List<object> urls { get; set; }
        public List<object> user_mentions { get; set; }
        public List<Medium> media { get; set; }
    }
    
    public class Metadata2
    {
        public string result_type { get; set; }
        public string iso_language_code { get; set; }
    }
    
    public class Description2
    {
        public List<object> urls { get; set; }
    }
    
    public class Url4
    {
        public string url { get; set; }
        public string expanded_url { get; set; }
        public string display_url { get; set; }
        public List<int> indices { get; set; }
    }
    
    public class Url3
    {
        public List<Url4> urls { get; set; }
    }
    
    public class Entities3
    {
        public Description2 description { get; set; }
        public Url3 url { get; set; }
    }
    
    public class User2
    {
        public int id { get; set; }
        public string id_str { get; set; }
        public string name { get; set; }
        public string screen_name { get; set; }
        public string location { get; set; }
        public string description { get; set; }
        public string url { get; set; }
        public Entities3 entities { get; set; }
        public bool @protected { get; set; }
        public int followers_count { get; set; }
        public int friends_count { get; set; }
        public int listed_count { get; set; }
        public string created_at { get; set; }
        public int favourites_count { get; set; }
        public int utc_offset { get; set; }
        public string time_zone { get; set; }
        public bool geo_enabled { get; set; }
        public bool verified { get; set; }
        public int statuses_count { get; set; }
        public string lang { get; set; }
        public bool contributors_enabled { get; set; }
        public bool is_translator { get; set; }
        public string profile_background_color { get; set; }
        public string profile_background_image_url { get; set; }
        public string profile_background_image_url_https { get; set; }
        public bool profile_background_tile { get; set; }
        public string profile_image_url { get; set; }
        public string profile_image_url_https { get; set; }
        public string profile_banner_url { get; set; }
        public string profile_link_color { get; set; }
        public string profile_sidebar_border_color { get; set; }
        public string profile_sidebar_fill_color { get; set; }
        public string profile_text_color { get; set; }
        public bool profile_use_background_image { get; set; }
        public bool default_profile { get; set; }
        public bool default_profile_image { get; set; }
        public bool following { get; set; }
        public bool follow_request_sent { get; set; }
        public bool notifications { get; set; }
    }
    
    public class Medium4
    {
        public int w { get; set; }
        public int h { get; set; }
        public string resize { get; set; }
    }
    
    public class Large2
    {
        public int w { get; set; }
        public int h { get; set; }
        public string resize { get; set; }
    }
    
    public class Thumb2
    {
        public int w { get; set; }
        public int h { get; set; }
        public string resize { get; set; }
    }
    
    public class Small2
    {
        public int w { get; set; }
        public int h { get; set; }
        public string resize { get; set; }
    }
    
    public class Sizes2
    {
        public Medium4 medium { get; set; }
        public Large2 large { get; set; }
        public Thumb2 thumb { get; set; }
        public Small2 small { get; set; }
    }
    
    public class Medium3
    {
        public long id { get; set; }
        public string id_str { get; set; }
        public List<int> indices { get; set; }
        public string media_url { get; set; }
        public string media_url_https { get; set; }
        public string url { get; set; }
        public string display_url { get; set; }
        public string expanded_url { get; set; }
        public string type { get; set; }
        public Sizes2 sizes { get; set; }
    }
    
    public class Entities4
    {
        public List<object> hashtags { get; set; }
        public List<object> symbols { get; set; }
        public List<object> urls { get; set; }
        public List<object> user_mentions { get; set; }
        public List<Medium3> media { get; set; }
    }
    
    public class RetweetedStatus
    {
        public Metadata2 metadata { get; set; }
        public string created_at { get; set; }
        public object id { get; set; }
        public string id_str { get; set; }
        public string text { get; set; }
        public string source { get; set; }
        public bool truncated { get; set; }
        public long? in_reply_to_status_id { get; set; }
        public string in_reply_to_status_id_str { get; set; }
        public int? in_reply_to_user_id { get; set; }
        public string in_reply_to_user_id_str { get; set; }
        public string in_reply_to_screen_name { get; set; }
        public User2 user { get; set; }
        public object geo { get; set; }
        public object coordinates { get; set; }
        public object place { get; set; }
        public object contributors { get; set; }
        public int retweet_count { get; set; }
        public int favorite_count { get; set; }
        public Entities4 entities { get; set; }
        public bool favorited { get; set; }
        public bool retweeted { get; set; }
        public bool possibly_sensitive { get; set; }
        public string lang { get; set; }
    }
    
    public class Status
    {
        public Metadata metadata { get; set; }
        public string created_at { get; set; }
        public object id { get; set; }
        public string id_str { get; set; }
        public string text { get; set; }
        public string source { get; set; }
        public bool truncated { get; set; }
        public long? in_reply_to_status_id { get; set; }
        public string in_reply_to_status_id_str { get; set; }
        public int? in_reply_to_user_id { get; set; }
        public string in_reply_to_user_id_str { get; set; }
        public string in_reply_to_screen_name { get; set; }
        public User user { get; set; }
        public object geo { get; set; }
        public object coordinates { get; set; }
        public object place { get; set; }
        public object contributors { get; set; }
        public int retweet_count { get; set; }
        public int favorite_count { get; set; }
        public Entities2 entities { get; set; }
        public bool favorited { get; set; }
        public bool retweeted { get; set; }
        public bool possibly_sensitive { get; set; }
        public string lang { get; set; }
        public RetweetedStatus retweeted_status { get; set; }
    }
    
    public class SearchMetadata
    {
        public double completed_in { get; set; }
        public long max_id { get; set; }
        public string max_id_str { get; set; }
        public string next_results { get; set; }
        public string query { get; set; }
        public string refresh_url { get; set; }
        public int count { get; set; }
        public int since_id { get; set; }
        public string since_id_str { get; set; }
    }
    
    public class RootObject
    {
        public List<Status> statuses { get; set; }
        public SearchMetadata search_metadata { get; set; }
    }
    

    我使用以下方法解析了您的数据

        public void PARSEVALUES(string jsonValue)//jsonValue contains your json
        {
            JavaScriptSerializer jss = new JavaScriptSerializer();
    
            RootObject r = jss.Deserialize<RootObject>(jsonValue);
    
            foreach (var tweetText in r.statuses)
            {
                string val = tweetText.text;
            }
        }
    

    【讨论】:

    • 如果您想基于 json 数据创建 C# ENTITY。你可以随时使用json2csharp.com
    • 您是否使用某种工具从 JSON 创建类?
    • 忽略以上内容,我发表评论时页面刷新
    • 是的,我使用过 json2csharp.com,我已经提到过。 @harag
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多