How do I de/serialize JSON in WinRT?

前端 未结 3 1211
灰色年华
灰色年华 2020-12-08 05:10

How do I take an object and convert it to a JSON string and then back into that object from a string, specifically, in WinRT for my Windows 8 Metro application?

3条回答
  •  误落风尘
    2020-12-08 06:04

    First generate C# classes with http://json2csharp.com/ Then use http://james.newtonking.com/pages/json-net.aspx for parsing

    My user class currently looks like this:

    public class User
    {
        public string id { get; set; }
        public string username { get; set; }
        public string full_name { get; set; }
        public string profile_picture { get; set; }
        public string bio { get; set; }
        public string website { get; set; }
        public Counts counts { get; set; }
    
        public static User SingleFromJSON(string jsonString)
        {
            return JsonConvert.DeserializeObject(jsonString).data;
        }
    
        public static User MultipleFromJSON(string jsonString)
        {
            return JsonConvert.DeserializeObject(jsonString).data;
        }
    
        private class SingleUser
        {
            public User data { get; set; }
        }
    
        private class MultipleUsers
        {
            public List data { get; set; }
        }
    }
    
    public class Counts
    {
        public int media { get; set; }
        public int follows { get; set; }
        public int followed_by { get; set; }
    }
    

    Super easy :D

提交回复
热议问题