c # मॉडल वर्ग में संख्या के रूप में json गुण?

Dec 06 2020

मैं एक मॉडल वर्ग का निर्माण कैसे कर सकता हूं, इस तरह से एक json का वर्णन करने के लिए: -

{"query":{"apikey":"65320-2d8b-11eb-864","continent":"Africa"},"data":{"0":{"country_id":1,"name":"Africa","country_code":null,"continent":"Africa"},"9":{"country_id":10,"name":"Algeria","country_code":"dz","continent":"Africa"},"11":{"country_id":12,"name":"Angola","country_code":"ao","continent":"Africa"},"23":{"country_id":24,"name":"Botswana","country_code":"bw","continent":"Africa"},"27":{"country_id":28,"name":"Cameroon","country_code":"cm","continent":"Africa"},"32":{"country_id":33,"name":"Congo","country_code":"cg","continent":"Africa"},"39":{"country_id":40,"name":"Egypt","country_code":"eg","continent":"Africa"},"48":{"country_id":49,"name":"Ghana","country_code":"gh","continent":"Africa"},"62":{"country_id":63,"name":"Ivory Coast","country_code":"ci","continent":"Africa"},"67":{"country_id":68,"name":"Kenya","country_code":"ke","continent":"Africa"},"80":{"country_id":81,"name":"Morocco","country_code":"ma","continent":"Africa"},"85":{"country_id":86,"name":"Nigeria","country_code":"ng","continent":"Africa"},"102":{"country_id":103,"name":"Rwanda","country_code":"rw","continent":"Africa"},"106":{"country_id":107,"name":"Senegal","country_code":"sn","continent":"Africa"},"111":{"country_id":112,"name":"South Africa","country_code":"za","continent":"Africa"},"115":{"country_id":116,"name":"Tanzania","country_code":"tz","continent":"Africa"},"118":{"country_id":119,"name":"Tunisia","country_code":"tn","continent":"Africa"},"120":{"country_id":121,"name":"Uganda","country_code":"ug","continent":"Africa"},"129":{"country_id":130,"name":"Zambia","country_code":"zm","continent":"Africa"},"130":{"country_id":131,"name":"Zimbabwe","country_code":"zw","continent":"Africa"}}}

समस्या "0", "9", "11" आदि की संख्या में है! मैं मॉडल वर्ग में इन संख्याओं को एक संपत्ति में कैसे बदल सकता हूं?

जवाब

2 pfx Dec 06 2020 at 21:37

यदि आप dataआइटम को Dictionaryअपने मॉडल पर लागू करते हैं , तो ये नंबर इस की कुंजी होंगे Dictionary

public class Model
{
    [JsonProperty("query")]
    public Query query {get; set; }

    [JsonProperty("data")]
    public Dictionary<string, Country> Data { get; set; }
}

public class Country
{
    [JsonProperty("country_id")]
    public int Id { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("country_code")]
    public string Code { get; set; }

    [JsonProperty("continent")]
    public string Continent { get; set; }
}

public class Query
{
    [JsonProperty("apikey")]
    public string ApiKey { get; set; }

    [JsonProperty("continent")]
    public string Continent { get; set; }
}
DerDingens Dec 06 2020 at 21:39

आप इन संख्याओं को शब्दकोश की कुंजी के रूप में मान सकते हैं। तो उन्हें इस तरह परिवर्तित कर सकते हैं:

using System;
using System.Collections.Generic;
using Newtonsoft.Json;
                    
public class Program
{
    public static void Main()
    {
        var testJson = "{\"data\":{\"11\": \"test11\"}}";
        var deserialized = (TestClass) JsonConvert.DeserializeObject(testJson, typeof(TestClass));
        Console.WriteLine(deserialized.Data[11]);
        
    }
}
public class TestClass
{
    public Dictionary<int, string> Data {get;set;}
}

( इस डॉटनेटफिल्ड उदाहरण को देखने के लिए परीक्षण करें )

स्पष्ट रूप से केवल तभी संभव है जब कोई संख्या दो बार या अधिक प्रकट न हो।