2

i m working on a .net project. I need to convert a json model (which has dynamicly changed numerical node name) to csharp class. Second node's name is changing dynamically, thats why i can not write a jsonproperty like this : [JsonProperty("353503")]. I did not see a json model like this. I tried many thing but couldn't achive. What should i do?

{
  "standings": {
    "353493": {
      "id": "353493",
      "name": "root name",
      "n": "0",
      "ut": "2021-08-23T20:54:12+00:00",
      "standing_participants": {
        "4273616": {
          "id": "4273616",
          "n": "14",
          "ut": "2021-08-23T20:54:11+00:00",
          "rank": "1",
          "standing_data": [
            {
              "id": "146906199",
              "standing_type_paramFK": "1",
              "value": "6",
              "code": "points",
              "n": "2",
              "ut": "2021-08-23T20:54:11+00:00",
              "sub_param": ""
            },
            {
              "id": "146906200",
              "standing_type_paramFK": "2",
              "value": "8",
              "code": "goalsfor",
              "n": "2",
              "ut": "2021-08-23T20:54:11+00:00",
              "sub_param": ""
            }
          ]
        },
        "4273617": {
          "id": "4273617",
          "n": "14",
          "ut": "2021-08-23T20:54:11+00:00",
          "rank": "2",
          "standing_data": [
            {
              "id": "146906198",
              "standing_type_paramFK": "1",
              "value": "6",
              "code": "points",
              "n": "2",
              "ut": "2021-08-23T20:54:11+00:00",
              "sub_param": ""
            },
            {
              "id": "146906201",
              "standing_type_paramFK": "2",
              "value": "8",
              "code": "goalsfor",
              "n": "2",
              "ut": "2021-08-23T20:54:11+00:00",
              "sub_param": ""
            }
          ]
        }
      }
    }
  }
}

When i try this, it works

 public partial class SampleStandingModel
    {
        [JsonProperty("standings")]
        public Standings Standings { get; set; }
    }

    public partial class Standings
    {
        [JsonProperty("353493")]
         public Standing Standing { get; set; }
    }

But when i remove [JsonProperty("353493")] line, it doesn't work

 public partial class SampleStandingModel
    {
        [JsonProperty("standings")]
        public Standings Standings { get; set; }
    }

    public partial class Standings
    {
        
         public Standing Standing { get; set; }
 //or this:
 //   public Dictionary<int, Standing> Standing { get; set; }
    
    }

my json parse code like below:

var deserializedObject = Newtonsoft.Json.JsonConvert.DeserializeObject(json);

The important point on my problem, second node (standing-> 353493) name is numerical and dynamic.

My question as marked answered before, but there is no same problem. Older problems are about dynamic types or objects. This problem about numerical and dynamic node name. I didn't find a solution for two days.

1 Answers1

1

You could try converting it to dynamic object using Json.Net:

dynamic stuff = JsonConvert.DeserializeObject("{ 'Name': 'Jon Doe' }");

or using Newtonsoft.Json.Linq:

dynamic stuff = JObject.Parse("{ 'Name': 'Jon Doe' }");

Additionally you could try converting it to Dictionary<string, string[]>: JsonSerializer.Deserialize<Dictionary<string,string[]>>(body, serializerOptions);

miursser
  • 121
  • 4
  • for more info look up this thread: https://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object – miursser Aug 24 '21 at 17:47
  • 2
    The problem I see with this is that even though the object gets deserialised, the OP would not be able to access any of those properties since their names are not constant. – bolkay Aug 24 '21 at 17:57
  • I've found possible duplicate with accepted answer: https://stackoverflow.com/questions/65700331/how-to-deserialize-dynamic-json-objects (I write here since i don't have enough reputation to comment on the question) – miursser Aug 24 '21 at 18:03