简体   繁体   中英

json_serializer.DeserializeObject, immediate window, VS2017

I am ac# beginner (but quite familiar with JavaScript), and I am learning by debugging example code. I have a question now regarding the "immediate window".

I set a breakpoint at

  (... nested object sent via JSON from some external javascript code ...)
var json_serializer = new JavaScriptSerializer();
var value_list = (IDictionary<string, object>)json_serializer.DeserializeObject(value);

Then evaluated the following in the immediate window

value_list
Count = 4
    [0]: {[type, msg]}
    [1]: {[settings, System.Collections.Generic.Dictionary`2[System.String,System.Object]]}
    [2]: {[hello, edge]}
    [3]: {[txt, notepad.exe]}

value_list["txt"]
"notepad.exe"


value_list["settings"]
Count = 2
    [0]: {[host, test.com]}
    [1]: {[port, 80]}

So far so good.

Then I tried

value_list["settings"]["host"]

But only got "error CS0021: Cannot apply indexing with [] to an expression of type 'object'".

How can I print the value of host in the immediate window?

C# is a strongly typed language.

You have

var value_list = (IDictionary<string, object>)json_serializer.DeserializeObject(value);

The var means that the type of the variable is determined by the compiler during compilation already. Because of the cast at the right side, the compiler determines this:

IDictionary<string, object> value_list = (IDictionary<string, object>)json_serializer.DeserializeObject(value);

So, value_list is of type IDictionary<string, object> . So, value_list["settings"] is of type object .

To see the value in the immediate window cast the intermediate result (which is of the type object ) to the appropriate type.

((IDictionary<string, object>)value_list["settings"])["host"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM