将json转换为自定义对象数组

Wil*_*lic 3 c# json reddit windows-phone-7

我正在尝试使用C#和Json.Net为Windows Phone 7创建一个reddit应用程序(仅用于练习).我试图把json转换成c#可以使用的东西,以及其他一些东西.我可以提取我想要的数据,但不知道我是否正在使用正确的方法.然后,一旦我有了json数据,我就无法将其转换为自定义对象.无论如何,我确信这段代码中有很多错误,所以任何帮助理顺我的标题都会非常感激.

这是代码:

   public partial class MainPage : PhoneApplicationPage
    {
        string json = "";
    RootObject topic = new RootObject();
    public MainPage()
    {
        InitializeComponent();
    }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        textBlock1.Text = "Retrieving...";
        string url = @"http://www.reddit.com/r/all.json";
        HttpWebRequest hWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
        hWebRequest.Method = "GET";
        hWebRequest.BeginGetResponse(Response_Completed, hWebRequest);
    }
    public void Response_Completed(IAsyncResult result)
    {
        HttpWebRequest request = (HttpWebRequest)result.AsyncState;
        HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result);
        using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
        {
            json = streamReader.ReadToEnd();
            topic = JsonConvert.DeserializeObject<RootObject>(json);

        }
        Deployment.Current.Dispatcher.BeginInvoke(() =>
        {
            //textBlock2.Text = topic.data2.title;
            textBlock1.Text = "Done.";
        });
Run Code Online (Sandbox Code Playgroud)

这一行告诉我需要"新"呼叫.

textBlock2.Text = topic.data2.title;
Run Code Online (Sandbox Code Playgroud)

现在这部分给了我麻烦.如何正确实例化数据,数据2和子项?

     public class MediaEmbed
    {
    }

    public class Data2
    {
        public string domain { get; set; }
        public MediaEmbed media_embed { get; set; }
        public object levenshtein { get; set; }
        public string subreddit { get; set; }
        public string selftext_html { get; set; }
        public string selftext { get; set; }
        public object likes { get; set; }
        public bool saved { get; set; }
        public string id { get; set; }
        public bool clicked { get; set; }
        public string title { get; set; }
        public object media { get; set; }
        public int score { get; set; }
        public bool over_18 { get; set; }
        public bool hidden { get; set; }
        public string thumbnail { get; set; }
        public string subreddit_id { get; set; }
        public string author_flair_css_class { get; set; }
        public int downs { get; set; }
        public bool is_self { get; set; }
        public string permalink { get; set; }
        public string name { get; set; }
        public double created { get; set; }
        public string url { get; set; }
        public string author_flair_text { get; set; }
        public string author { get; set; }
        public double created_utc { get; set; }
        public int num_comments { get; set; }
        public int ups { get; set; }
        //public override string ToString()
        //{
        //    return title;
        //}
    }

    public class Child
    {
        public string kind { get; set; }
        public Data2 data { get; set; }
    }

    public class Data
    {
        public string modhash { get; set; }
        public Child[] children { get; set; }
        public string after { get; set; }
        public object before { get; set; }
    }

    public class RootObject
    {
        public RootObject()
        {
        }
        public string kind { get; set; }
        public Data data = new Data();
        public Data2 data2 = new Data2();
        public Child child = new Child();
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 5

我看到你有什么问题.第一个是您必须将json数据反序列化的类的结构.为此手动编写类是非常繁琐且容易出错的,因此我通常喜欢使用此站点插入json并获取我需要的类并根据需要重命名.然后我去找数据的api文档来填补数据中根本没有的类中的任何空白.

之后,您只需要反序列化您现在拥有的根对象类的单个实例,而不是它们的List,如下所示:

RootObject topics = JsonConvert.DeserializeObject<RootObject>(json);
Run Code Online (Sandbox Code Playgroud)

另外需要注意的是,当您对数据进行反序列化时,在您的代码中,您当前正在声明一个新的局部变量topics,该变量会隐藏您topics在该特定范围内也调用的成员变量,因此如果您检查该成员变量,你会发现它里面没有任何数据.你不想在那里声明一个新的变量,你只想使用现有的成员变量,所以你只想这样做而不是前一行:

topics = JsonConvert.DeserializeObject<RootObject>(json);
Run Code Online (Sandbox Code Playgroud)

或者即使this.topics您希望真正明确并清楚该变量的来源.