0

How do I Deserialize the following. The problem is that the variable name is a number. So how should MyClass be defined?

json_str:
{"23521952": {"b": [], "o": []}, "23521953": {"b": [], "o": []}}

class MyClass {     //? };

var var = JsonConvert.DeserializeObject<MyClass>(json_str);
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
petke
  • 1,345
  • 10
  • 25
  • 2
    There must be thousands of examples of how to deserialise JSON, including website that even give you the code. Have you even tried to search for a solution? – DavidG Mar 15 '18 at 16:28
  • 3
    https://stackoverflow.com/questions/24218536/deserialize-json-that-has-some-property-name-starting-with-a-number – Anton Komyshan Mar 15 '18 at 16:28
  • Honestly, i'm not sure how the javascript deserializer will handle that. My guess, it would throw an error. If you're lucky, it might add some kind of identifier to make it a valid variable name. But, C# doesn't allow variables that start with numbers, thus number-only variables are also not allowed. So, no way to reference it. – Erik Funkenbusch Mar 15 '18 at 16:31
  • The example in the link wont work as the variable name is not always say "23521952", it can be any number. – petke Mar 15 '18 at 16:31
  • 2
    @someoneelse so if the numbers are random: should this actually be a dictionary? this feels like a `Dictionary` – Marc Gravell Mar 15 '18 at 16:32
  • You can probably deserialize to `Dictionary` where `MyClass` will have properties for `b` and `o` – Jonathon Chase Mar 15 '18 at 16:32
  • Thanks marc. You are correct. – petke Mar 15 '18 at 16:34
  • Possible duplicate of [Deserialize json that has some property name starting with a number](https://stackoverflow.com/questions/24218536/deserialize-json-that-has-some-property-name-starting-with-a-number) – trailmax Mar 15 '18 at 16:45

2 Answers2

5

This sounds like the outer object is actually a dictionary:

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

class Foo
{
    // no clue what b+o look like from the question; modify to suit
    public int[] b { get; set; }
    public string[] o { get; set; }
}
static class P
{
    static void Main()
    {
        var json = @"{""23521952"": {""b"": [], ""o"": []}, ""23521953"": {""b"": [], ""o"": []}}";
        var obj = JsonConvert.DeserializeObject<Dictionary<string, Foo>>(json);
        foreach(var pair in obj)
        {
            System.Console.WriteLine($"{pair.Key}, {pair.Value}");
        }

    }
}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
1

You can use anonymous type deserialization for your data like this, without creating classes for properties of JSON. Hope it works.

    var finalResult=JsonConvert.DeserializeAnonymousType(
            json_str,  // input
            new 
            {
              Id= 
              {
                new
                { 
                   b=new[], o=new[]
                }
              }
            }
          ); 

    foreach(var id in finalResult.Id)
    {
    console.write(id); // gives ids like 23521952
    console.write(id.b[0]) // gives first elemnt in 'b' array
    }
Mahender Kvs
  • 174
  • 2
  • 7