Deserializing JSON without a unique property name

I have a json string that looks like this

{
    "1ST": {
        "name": "One",
        "symbol": "1st"
    },
    "2ND": {
        "name": "Two",
        "symbol": "2nd"
    }
}

I am trying to Serialize it to a C # object. It looks like it is creating a dictionary, so I created the following structure

public class Response
{
    public Dictionary<string, Item> Objects { get; set; }
}

public class Item
{
    public string name { get; set; }
    public string symbol { get; set; }
}

And during serialization, the following

response = JsonConvert.DeserializeObject<Response>(jsonString);

It does not cause an error during deserialization, but my answer is returned only as null. What am I missing?

+4
source share
2 answers

You have the right basic idea, but you have an additional property Objectsthat you really don't need: your JSON is a dictionary, efficiently. You can deserialize it directly on Dictionary<string, Item>>. Here is an example:

using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;

public class Item
{
    public string Name { get; set; }
    public string Symbol { get; set; }

    public override string ToString() => $"{Name}/{Symbol}";
}

public class Test
{
    static void Main()
    {
        var json = File.ReadAllText("test.json");
        var dictionary = JsonConvert.DeserializeObject<Dictionary<string, Item>>(json);
        foreach (var entry in dictionary)
        {
            Console.WriteLine($"{entry.Key}: {entry.Value}");
        }
    }
}

Conclusion:

1ST: One/1st
2ND: Two/2nd

, Response, :

  • Dictionary<string, Item> Objects. ( , Objects.)
  • , Response . ( , .)
  • , - Json.NET Objects "" .

, . Objects Dictionary<string, JToken>, [JsonExtensionData], . Json.NET . , JToken Item ( Json.NET) , , , . , Json.NET .

+1

, , .

, .

, , #.

using Newtonsoft.Json;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var jsonString = @"{
                            'First': {
                                'name': 'One',
                                'symbol': '1st'
                            },
                            'Second': {
                                'name': 'Two',
                                'symbol': '2nd'
                            }
                        }";

            //If you want to access using strong type.
            var response = JsonConvert.DeserializeObject<Response>(jsonString);
            var secondSymbol = response.Second.symbol.ToString();
            System.Console.WriteLine(secondSymbol);

            //If you want to access using dynamic type, will evalute in runtime.
            var responseDynamic = JsonConvert.DeserializeObject<dynamic>(jsonString);
            var secondSymbolDynamic = responseDynamic.Second.symbol.ToString() ;
            System.Console.WriteLine(secondSymbolDynamic);

            System.Console.ReadLine();
        }

        public class Response
        {
            public Item First { get; set; }
            public Item Second { get; set; }
        }

        public class Item
        {
            public string name { get; set; }
            public string symbol { get; set; }
        }
    }
}

, .:)

0

Source: https://habr.com/ru/post/1696339/


All Articles