Moon
Moon

Reputation: 20022

System.Collections.Generic.Dictionary : how to access its members

i am learning, i have response from a REST API in jSON format and it is being recieved in s string named filters

what i am trying to do is de-serialize it so that i may use the data contained in jSON.. this is what i am doing

JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
var deserializedJson = jsSerializer.DeserializeObject(filters);

now the thing is it is working but i don;t know how to further utilize it...

i debug-ed the de-serialization statement to find the following result

enter image description here

it says that it is that my reult object is of {System.Collections.Generic.Dictionary<string,object>} type

enter image description here

what to do... i need extract the property : Make and value : Honda

there is the jSON which is being sent.. that is what is in filters sting [{"property":"Make","value":"Honda"}]

Upvotes: 1

Views: 5734

Answers (2)

Mahesh
Mahesh

Reputation: 150

Another option would be as below,

dynamic deserializedJson = jsSerialize.DeserializeObject(filters);
object prop = deserializedJson["property"];
object make = deserializedJson["value"];

Upvotes: 0

Alexander
Alexander

Reputation: 4173

First, you need to cast returned object to Dictionary, like this:

var deserializedJson = jsSerialize.DeserializeObject(filters);
var dictionary = (Dictionary<string,object>)deserializedJson;

Now, you can access any property by key:

object prop = dictionary["property"];
object make = dictionary["value"];

Note that "prop" and "make" are object variables, but I suspect that underlying type is a string. So you can just call .ToString() on both property and make to convert them to string.

Upvotes: 2

Related Questions