Deserialize JSON string to Dictionary<string,object>

Total Post:135


 3497  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 ?

  1. Re: Deserialize JSON string to Dictionary<string,object>

    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)


Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.