序列化/反序列化Wot Json API

时间:2016-10-24 13:59:53

标签: c# json serialization deserialization

我有以下Json:https://www.mywot.com/wiki/API

{
    "google.com": {
        "target": "google.com",
        "0": [94, 73],
        "1": [94, 73],
        "2": [94, 73],
        "4": [93, 67],
        "categories": {
            "501": 99,
            "301": 48,
            "304": 5
        }
    },
    "yahoo.com": {
        "target": "yahoo.com",
        "0": [94, 75],
        "1": [94, 75],
        "2": [94, 75],
        "4": [93, 69],
        "categories": {
            "501": 99,
            "301": 16,
            "304": 11
        }
    }
}

我想使用C#MVC Model + Json.net来序列化和反序列化Json。 我试过以下模型,但我有两个问题:

  1. 网站名称是变量(google.com,yahoo.com)
  2. 类别键是数字(MVC模型不允许数字作为键)。
  3. public class Categories
    {
        public int 401 { get; set; }
        public int 501 { get; set; }
    }
    
    public class Website 
    {
        public string target { get; set; }
        public List<int> 0 { get; set; }
        public List<int> 1 { get; set; }
        public List<int> 2 { get; set; }
        public List<int> 4 { get; set; }
        public Categories categories { get; set; }
    }
    
    public class RootObject
    {
        public Website domain_name { get; set; }
    } 
    

1 个答案:

答案 0 :(得分:2)

所以你的json很脏。而你不能像0,1,2,3那样为变量设置名称。 在这种情况下,您需要使用[JsonProperty("HERE_IS_PROPERTY_NAME")]属性。

下面的代码可以解析你的json。

 class Program
    {
        static void Main(string[] args)
        {
            string json = "{\"google.com\":{\"target\":\"google.com\",\"0\":[94,73],\"1\":[94,73],\"2\":[94,73],\"4\":[93,67],\"categories\":{\"501\":99,\"301\":48,\"304\":5}},\"yahoo.com\":{\"target\":\"yahoo.com\",\"0\":[94,75],\"1\":[94,75],\"2\":[94,75],\"4\":[93,69],\"categories\":{\"501\":99,\"301\":16,\"304\":11}}}";

            Dictionary<string, dynamic> dictionary_data = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(json);
            List<Website> sites = new List<Website>();
            foreach (var item in dictionary_data)
            {
                string data = Convert.ToString(item.Value);
                data = data.Replace("\r\n", string.Empty); ;
                Website site = JsonConvert.DeserializeObject<Website>(data);
                sites.Add(site);
            }
        }
    }


    public class Categories
    {
        [JsonProperty("401")]
        public int a { get; set; }
        [JsonProperty("501")]
        public int b { get; set; }
    }
    public class Website
    {
        public string target { get; set; }

        [JsonProperty("0")]
        public List<int> FirstList { get; set; }

        [JsonProperty("1")]
        public List<int> SecondList { get; set; }

        [JsonProperty("2")]
        public List<int> ThirdList { get; set; }

        [JsonProperty("4")]
        public List<int> FourList { get; set; }
        public Categories categories { get; set; }
    }

    public class RootObject
    {
        public Website domain_name { get; set; }
    }