Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserializing a JSON with variable name/value pairs into object in C#

I am using C# .NET 4.0 to parse a JSON into a custom object. I am using JavaScriptSerializer.Deserialize to map it to a class that I wrote. Problem is, the JSON's name/value pairs are not static and vary depending on the argument isChain, as seen in this JSON fragment (better link at bottom):

{
   "STATE_WALK_LEFT":{
      "isChain":"1",
      "x":"1"
   },
   "STATE_WALK_LEFT_0":{
      "x":"0"
   },
   "STATE_WALK_LEFT_1":{
      "x":"40"
   },
   "STATE_WALK_LEFT_2":{
      "x":"80"
   },
   "STATE_WALK_RIGHT":{
      "isChain":"0"
   },
   "STATE_RUN_LEFT":{
      "isChain":"0"
   }
}

The chains can have anywhere from _STATE_0 to _STATE_25 entries in the chains. Is there some way to store this data so I don't have to write 12*26 empty classes like so:

public StateWalkLeft0 STATE_WALK_LEFT { get; set; }
public StateWalkLeft0 STATE_WALK_LEFT_0 { get; set; }
public StateWalkLeft1 STATE_WALK_LEFT_1 { get; set; }
public StateWalkLeft2 STATE_WALK_LEFT_2 { get; set; }
public StateWalkLeft3 STATE_WALK_LEFT_3 { get; set; }

Is there a library or some other way I could use to partially parse only the STATE_0, STATE_1, etc fields? Could you maybe suggest a way to add these recently added JSON pairs?

Edited to clarify: To get an idea of what I'm working with, here is the Class derived from the JSONs:

Check out my full Class to get an idea of what the JSONs contain

Basically, I just need a way to store these recently implemented chains in this class somehow for processing. All of those classes/properties are generated from these JSONs.

like image 243
Brad C Avatar asked May 11 '12 17:05

Brad C


People also ask

How do you serialize and deserialize an object in C# using JSON?

To serialize a . Net object to JSON string use the Serialize method. It's possible to deserialize JSON string to . Net object using Deserialize<T> or DeserializeObject methods.

What is serialized and deserialized in JSON?

JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object).


1 Answers

Use Newtonsoft Json.NET and as example following code

internal struct ChainX
{
  public int x { get; set; }
  public int isChain { get; set; }
}

    static string json = 
@"{
   ""STATE_WALK_LEFT"":{
      ""isChain"":""1"",
      ""x"":""1""
   },
   ""STATE_WALK_LEFT_0"":{
      ""x"":""0""
   }, 
   ""STATE_WALK_LEFT_1"":{
      ""x"":""40""
   },
   ""STATE_WALK_LEFT_2"":{
      ""x"":""80""
   },
   ""STATE_WALK_RIGHT"":{
      ""isChain"":""0""
   },
   ""STATE_RUN_LEFT"":{
      ""isChain"":""0""
   }
}";

and a line of code to deserialize to Dictionary:

var values = JsonConvert.DeserializeObject<Dictionary<string, ChainX>>(json);

after that you can simple access values by dictionary key:

ChainX valueWalkLeft1 = values["STATE_WALK_LEFT_1"];
like image 192
Regfor Avatar answered Oct 03 '22 06:10

Regfor