0

I come across some specially formatted JSON syntaxes from a web. Basically we get variable number of properties with dots in the name. Here are the two obvious cases:

case 1:

"data" {
    "SET.Key.count":"0",
    "SET.Value.count":"0",
    ...
}

case 2:

"data" {
    "SET.Key.0":"Key 1",
    "SET.Key.1":"Key 2",
    "SET.Key.2":"Key 3",
    "SET.Key.3":"Key 4",
    "SET.Key.count":"4",
    "SET.Value.0":"10",
    "SET.Value.1":"20",
    "SET.Value.2":"30",
    "SET.Value.3":"40",
    "SET.Value.count":"4",
    ...
}

Where count number 4 is an arbitrary number. They can be anything, but key count and value count should be same. "..." means more properties but they can be ignored for this post.

I deserialize JSON streams with Newtonsoft.Json and C#. But I don't have to stay with Newtonsoft.Json. How can I deserialize it into a list of objects (key and value). Any suggestions to handle this kind of JSON properties will be greatly appreciated.

Louis
  • 660
  • 1
  • 7
  • 14
  • 1
    Are you getting erros? Are you trying to do anything from the dots? Your question is not very clear. – Daniel Möller May 10 '18 at 03:56
  • 1
    What exactly is the question? – ReyHaynes May 10 '18 at 03:56
  • Looks like you need to use some sort of dictionary, e.g. `public Dictionary data { get; set; }`. See [How can I parse a JSON string that would cause illegal C# identifiers?](https://stackoverflow.com/a/24536564/3744182) or [Create a strongly typed c# object from json object with ID as the name](https://stackoverflow.com/a/34213724/3744182) – dbc May 10 '18 at 03:57
  • You can create dictionary from this JSON in C#. You will need to implement your own JsonContractResolver for this. Posting from phone so class name may not be accurate. – danish May 10 '18 at 04:05
  • Thanks for the above comments. I have added more explanations. – Louis May 10 '18 at 12:16

1 Answers1

0

Use JsonPropertyAttribute

 [JsonProperty(PropertyName = "user.isSystem")]
 public int IsSystem { get; set; }

 [JsonProperty(PropertyName = "0")]
 public int ZeroField{ get; set; }
Hesam Faridmehr
  • 1,176
  • 8
  • 20
  • This only solves dot notations, but it won't take care of variable numbers of properties. Thanks for the reply. – Louis May 10 '18 at 12:19