Deserialise JSON containing numeric key with Json.NET

后端 未结 3 1963
孤街浪徒
孤街浪徒 2021-01-14 18:50

I would like to deserialize the following JSON (using Json.NET) to an object, but cannot, as the class name would need to begin with a number.

An example of this is

相关标签:
3条回答
  • 2021-01-14 19:18

    It sounds like the Pages property in your Query class would just need to be a Dictionary<int, Page> or Dictionary<string, Page>.

    Complete example with the JSON you've provided - I've had to guess at some of the name meanings:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using Newtonsoft.Json;
    
    public class Root
    {
        [JsonProperty("batchcomplete")]
        public string BatchComplete { get; set; }
        [JsonProperty("continue")]
        public Continuation Continuation { get; set; }
        [JsonProperty("query")]
        public Query Query { get; set; }
    }
    
    public class Continuation
    {
        [JsonProperty("grncontinue")]
        public string GrnContinue { get; set; }
        [JsonProperty("continue")]
        public string Continue { get; set; }
    }
    
    public class Query
    {
        [JsonProperty("pages")]
        public Dictionary<int, Page> Pages { get; set; }
    }
    
    public class Page
    {
        [JsonProperty("pageid")]
        public int Id { get; set; }
        [JsonProperty("ns")]
        public int Ns { get; set; }
        [JsonProperty("title")]
        public string Title { get; set; }
        [JsonProperty("extract")]
        public string Extract { get; set; }
    }
    
    class Test
    {
        static void Main()
        {
            string text = File.ReadAllText("test.json");
            var root = JsonConvert.DeserializeObject<Root>(text);
            Console.WriteLine(root.Query.Pages[16689396].Title);
        }    
    }
    
    0 讨论(0)
  • 2021-01-14 19:32

    Related question: Json deserialize from wikipedia api with c#

    Essentially you need to changes from using a class for the pages to a dictionary, which allows for the dynamic nature of the naming convention.

    Class definitions :

    public class pageval
    {
        public int pageid { get; set; }
        public int ns { get; set; }
        public string title { get; set; }
        public string extract { get; set; }
    }
    
    public class Query
    {
        public Dictionary<string, pageval>  pages { get; set; }
    }
    
    public class Limits
    {
        public int extracts { get; set; }
    }
    
    public class RootObject
    {
        public string batchcomplete { get; set; }
        public Query query { get; set; }
        public Limits limits { get; set; }
    }
    

    Deserialization :

    var root = JsonConvert.DeserializeObject<RootObject>(__YOUR_JSON_HERE__);
    var page = responseJson.query.pages["16689396"];
    
    0 讨论(0)
  • 2021-01-14 19:33

    You can implement your own DeSerializer or editing the JSON before you DeSerialize it.

    0 讨论(0)
提交回复
热议问题