DESERIALIZE JSON STRING TO DICTIONARY<STRING,OBJECT>

Chintoo Semi

Total Post:135

Points:947
Posted by  Chintoo Semi
C# 
C#
 2684  View(s)
Ratings:
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. Pravesh Singh

    Post:412

    Points:2888
    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)

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!