【发布时间】:2018-12-31 12:33:10
【问题描述】:
我知道这是一个旧栗子,但我想这样做 而不 导入 newton-soft 或 json.net
我知道这应该可行
这是 json:
{ "do": "Thing", "with": "abc" }
字面意思就是这样。我需要把它带入 c# 领域
这是我目前所拥有的
var json = wc.DownloadString("url");
Console.WriteLine("GOT >> " + json); //says GOT >> { "do": "Thing", "with": "abc" }
var sJson = new JavaScriptSerializer();
var data = sJson.Deserialize<Dictionary<string, string>[]>(json); //crashes with No parameterless constructor defined for type of 'System.Collections.Generic.Dictionary
从我的单行 json 中获取 data["do"] 和 data["with"] 的最精简、最不臃肿的方式是什么?它只会返回一件事......如果我必须串走它,我会的,但它不应该这么难
【问题讨论】:
-
为什么是
Dictionary<string, string>[]数组?你所拥有的充其量是Dictionary<string, string> -
因为“do”和“with”,但是想想我不需要那个,原来里面有一个数组用于with,我把它切碎了一点
-
我最后是这样做的
Do data = sJson.Deserialize<Do>(json);其中Do只是public string todo = ""; public string with = "";似乎仍然非常不理想,但是嘿嘿 -
“做”和“与”什么? :) 您的示例 JSON 中没有数组,因此它对应于
Dictionary<string, string>,“do”和“with”是键,“Thing”和“abc”是它们对应的值。 -
@MrHeelis 检查provided answer
标签: c# json dictionary