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 ?
See mridula's answer for why you are getting null. But if you want to directly convert the json string to dictionary you can try following code snippet.
Dictionary<string, object> values = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
I like this method:
using Newtonsoft.Json.Linq; //jsonString is your JSON-formatted string JObject jsonObj = JObject.Parse(jsonString); Dictionary<string, string> dictObj = jsonObj.ToObject<Dictionary<string, object>>();
You can now access anything you want using the dictObj
as a dictionary. You can also use Dictionary<string, string>
if you prefer to get the values as strings.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With