2

我正在使用以下教程来解析 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 中的一个数组,我希望这就是它不起作用的原因。有没有人有任何想法?谢谢您的帮助!

4

2 回答 2

2

您可以使用 LINQ to JSON,如下所示:

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

阅读LINQ to JSON文档以获取有关如何查询所需 JSON 片段的详细信息。

于 2013-08-08T12:05:52.977 回答
0

您可以简单地复制粘贴下面的代码并获得答案。这就是我解析你的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;
        }
    }
于 2013-08-08T13:08:46.937 回答