1

I have JSON with Dictionary of string-key with value-arrays. There are his structure below:

{
"NameA":{
    "ParametersA":[
        [104.3,5.368783],
        [104.212,2.57357],...,
    ],
    "ParametersB":[
        [104.3,5.368783],
        [104.212,2.57357],...,
    ]
},
"NameB":{
    "ParametersA":[
        [104.3,5.368783],
        [104.212,2.57357],...,
    ],
    "ParametersB":[
        [104.3,5.368783],
        [104.212,2.57357],...,
        ]
    },
"ThousandsNamesN":{[...]
    }
}

I create class for this to obtain data like this: Dictionaty<key=NameA, value=List<Parameters>> and parameters is another class with to arrays A and B.

This is my Root class:

    internal class RawDepth
{
    public Dictionary<string, Parameters> Names { get; set; }

    internal class Parameters
    {
        [JsonProperty("ParametersA")]
        public IList<Orders> A { get; set; }


        [JsonProperty("ParametersB")]
        public IList<ParamsArray> B { get; set; }
    }

    internal class ParamsArray
    {
        public decimal[,] _Orders { get; set; }
    }
}

I catchs a null reference exception. I tried create class different ways, but I still can't deserialise it. What I doing wrong?

1
  • Seperate your classes. Commented Sep 22, 2017 at 16:29

2 Answers 2

1

Your model can be simply something like this

public class Parameters
{
    public List<List<decimal>> ParametersA { get; set; }
    public List<List<decimal>> ParametersB { get; set; }
}

Now you can deserialize as

var dict = JsonConvert.DeserializeObject<Dictionary<string, Parameters>>(json);
Sign up to request clarification or add additional context in comments.

Comments

1

There is a great tool json2csharp that lets you paste JSON and it generates C# classes from it. That's what I got from your sample:

public class NameA
{
    public List<List<double>> ParametersA { get; set; }
    public List<List<double>> ParametersB { get; set; }
}

public class NameB
{
    public List<List<double>> ParametersA { get; set; }
    public List<List<double>> ParametersB { get; set; }
}

public class RootObject
{
    public NameA NameA { get; set; }
    public NameB NameB { get; set; }
    public List<string> ThousandsNamesN { get; set; }
}

I think we can safely assume that NameA and NameB classes are the same.

I had to edit JSON a little fot it to work, so I will paste it below:

    {
"NameA":{
    "ParametersA":[
        [104.3,5.368783],
        [104.212,2.57357],
    ],
    "ParametersB":[
        [104.3,5.368783],
        [104.212,2.57357],
    ]
},
"NameB":{
    "ParametersA":[
        [104.3,5.368783],
        [104.212,2.57357],
    ],
    "ParametersB":[
        [104.3,5.368783],
        [104.212,2.57357],
        ]
    },
"ThousandsNamesN":["name1", "name2"]

}

So after taking into consideration what was your initail idea, your code should look like that:

public class Order
{
    public List<List<double>> ParametersA { get; set; }
    public List<List<double>> ParametersB { get; set; }
}

var obj = JsonConvert.DeserializeObject<Dictionary<string, Order>>(json);

2 Comments

I know, use the same service. But I have Thousands keys NameA, NameB, ...NameN where N can be 3000-4000. Early I deserialise same JSON to Dictionary where NameA..N was in another object, but in this JSON i have troubles...
I've edited my answer. You root object is a dictionary

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.