简体   繁体   中英

JSON Deserialization C# into Dictionary

I am trying to deserialize the following data format, into some kind of data structure using Dictionaries, in C#.

The structure is the following:

"Obj1": {
    "Value": 0.6,
    "Data": {
        "Data1": 0.1,
        "Data2": 0.2,
        "Data3": 0.3,
        ...
    }
},
...

Does anyone have any idea on how I could accomplish this?

Thanks.

 Create Below Class and Deserialize it, but keep in mind this type of JSON not allowed in any language. See Data Objects it's in reapeated manners so none of object Deserialize it.

 public class Data
 {
  public double Data1 { get; set; }
  public double Data2 { get; set; }
  public double Data3 { get; set; }
 }

 public class Obj1
 {
   public double Value { get; set; }
   public Data Data { get; set; }
 }

public class RootObject
{
   public Obj1 Obj1 { get; set; }
}

Assuming your json is an array like

[
    {
        "Obj1": {
            "Value": 0.6,
            "Data": {
                "Data1": 0.1,
                "Data2": 0.2,
                "Data3": 0.3
            }
        }
    }
]

putting it into a dictionary is very simple with JSON.net

public class RootObject
{
    public Obj1 Obj1 { get; set; }
}

public class Obj1
{
    public double Value { get; set; }
    public Data Data { get; set; }
}

public class Data
{
    public double Data1 { get; set; }
    public double Data2 { get; set; }
    public double Data3 { get; set; }
}


string json = "[{\"Obj1\":{\"Value\":0.6,\"Data\":{\"Data1\":0.1,\"Data2\":0.2,\"Data3\":0.3}}}]";

List<RootObject> result = JsonConvert.DeserializeObject<List<RootObject>>(json);
Dictionary<double, List<double>> dict = result.ToDictionary(key => key.Obj1.Value, value => new List<double> {value.Obj1.Data.Data1, value.Obj1.Data.Data2, value.Obj1.Data.Data3});

Newtonsoft Json.NET is The One.

You can download the .dll file and "add reference" or install it using Nuget in Visual studio. Then you could use it like this:

Dictionary<string,object> dict = JsonConvert.DeserializeObject<Dictionary<string,object>>(json);

Here is an example using Newtonsoft.Json

Create your type first:

public class Obj1
{
    public string Value { get; set; }
    public Dictionary<string,double> Data { get; set; }
}

Then use the following to serialize and deserialize

        //Json String
        var json = "{\"Value\":\"0.6\",\"Data\":{\"Data1\":0.1,\"Data2\":0.3,\"Data3\":0.3}}";

        //If you want to deserialize
        var objDeserialized = JsonConvert.DeserializeObject<Obj1>(json);


        //If you want to Serialize
        var objToSerialize = new Obj1()
        { 
            Value = "0.6",
            Data = new Dictionary<string, double>()
            {
                {"Data1",0.1 },
                {"Data2",0.3},
                {"Data3",0.3 }
            }
        };

        var serializedObj = JsonConvert.SerializeObject(objToSerialize);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM