How to deserialize variable size JSON string with variable names
Asked Answered
R

1

1

Deserializing a small, fixed size, fixed structure, with fixed field names, JSON string is easy: Just define a class that contains all the fields (with correct types and names, all known at compile time).

Deserializing a variable-size of repeating nested pairs, is somewhat more challenging but can be done with the help of a List<> inside the class.

But what do I do when the name of the fields are unknown at compile time? e.g.:

{
   "container":{
      "GD01AB491103":{
         "field_id1":11,
         "field_id2":12,
         "field_id3":13,
         "field_id4":"fourteen"
      },
      "DC01AB491103":{
         "field_id1":21,
         "field_id2":22,
         "field_id3":23,
         "field_id4":"twenty four"
      },
      "GH01AB451101":{
         "field_id1":31,
         "field_id2":32,
         "field_id3":33,
         "field_id4":"thirty four"
      }
      .
      .
      .
   },
   "terminator"
}

How to deserialize such a string?

(preferably with .NET's JavaScriptSerializer but if it's too weak/incapable, I might need to resort to something else)

Edit: To clarify the nature of the challenge: In the example above, in order to define a class:

public class ??????
{
    public int field_id1  {get;set;}
    public int field_id2  {get;set;}
    public int field_id3  {get;set;}
    public string field_id4 {get;set;}
}

I need to query the JSON string first, then at runtime build classes (reflection?) with these variable-name class objects in it? Looks too cumbersome... Perhaps there is a saner way?

Or maybe the class/field names are irrelevant to .NET's JavaScriptSerializer and all matters is the type? (and correct structure of course)

Reinaldoreinaldos answered 25/10, 2012 at 15:51 Comment(0)
N
3

You can do this probably more simply than you think.. your ?????? class can be anything..

public class GenericObject
{
    public int field_id1  {get;set;}
    public int field_id2  {get;set;}
    public int field_id3  {get;set;}
    public string field_id4 {get;set;}
}

and then deserialize the root of the object graph into an object that contains a Dictionary<string,GenericObject>...

public class SomeContainer
{
    public Dictionary<string,GenericObject> container {get;set;}
}

you can then loop over the values of the dictionary if you don't care about the names of the keys.

Nephro answered 25/10, 2012 at 16:13 Comment(3)
Thanks. In my quest for ways to make this process simpler I found this great site which automated C# class building from a given JSON string: json2csharp.comReinaldoreinaldos
Actually, it works with both JSON.net and .NET's JavaScriptSerializer.Reinaldoreinaldos
Yeah it should be pretty serializer independent the one concern I had was with the trailing "terminator" entry because it isn't valid JSON but as long as that is only a hint then you should be fine. FWIW if you have the option (and not too nuanced .net generics) I would recommend JSON.net over JavaScriptSerializer for performance reasons.Nephro

© 2022 - 2024 — McMap. All rights reserved.