Home > DeveloperSection > Forums > Deserialize JSON string to Dictionary<string,object>
Chintoo Semi

Total Post:135

Posted on    December-23-2013 2:09 AM

 C# C# 

 1 Reply(s)
 2010  View(s)
Rate this:

I have this string:

[{ "processLevel" : "1" , "segments" : [{ "min" : "0", "max" : "600" }] }]

I'm deserializing the object:

object json = jsonSerializer.DeserializeObject(jsonString);

The object looks like:

object[0] = Key: "processLevel", Value: "1"

object[1] = Key: "segments", Value: ...

And trying to create a dictionary:

Dictionary<string, object> dic = json as Dictionary<string, object>;

but dic gets null.

What can be the issue ?

Pravesh Singh

Total Post:411

Posted on    December-23-2013 4:54 AM

Hi Chintoo,

The problem is that the object is not of type Dictionary<string,object> or a compatible type, thus you can't cast directly. I would create a custom object and use Deserialize.

public class DeserializedObject{

    public string processLevel{get;set;}

    public object segments{get;set}


IEnumerable<DeserializedObject> object=jsonSerializer.Deserialize<IEnumerable<DeserializedObject>>(json)

Don't want to miss updates? Please click the below button!

Follow MindStick