简体   繁体   English

json_serializer.DeserializeObject,立即窗口,VS2017

[英]json_serializer.DeserializeObject, immediate window, VS2017

I am ac# beginner (but quite familiar with JavaScript), and I am learning by debugging example code.我是 ac# 初学者(但对 JavaScript 非常熟悉),我正在通过调试示例代码来学习。 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'".但只得到“错误 CS0021:无法将 [] 索引应用于‘对象’类型的表达式”。

How can I print the value of host in the immediate window?如何在即时窗口中打印主机的值?

C# is a strongly typed language. C# 是一种强类型语言。

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. var表示变量的类型已经在编译期间由编译器确定。 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> .因此, value_list的类型为IDictionary<string, object> So, value_list["settings"] is of type object .因此, value_list["settings"]object类型。

To see the value in the immediate window cast the intermediate result (which is of the type object ) to the appropriate type.要查看立即窗口中的值,请将中间结果(类型为object )转换为适当的类型。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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