如何将 JSON 反序列化为一个简单的 Dictionary<string,string>在 ASP.NET 中? [英] How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?

查看:20
本文介绍了如何将 JSON 反序列化为一个简单的 Dictionary<string,string>在 ASP.NET 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 JSON 键/值列表,通过 POST 发送回 ASP.NET.示例:

I have a simple key/value list in JSON being sent back to ASP.NET via POST. Example:

{ "key1": "value1", "key2": "value2"}

我不想反序列化为强类型的 .NET 对象

我只需要一个普通的 Dictionary(Of String, String),或一些等效的(哈希表、Dictionary(Of String, Object)、老式的 StringDictionary——地狱,一个 2-D 字符串数组对我有用.

I simply need a plain old Dictionary(Of String, String), or some equivalent (hash table, Dictionary(Of String, Object), old-school StringDictionary--hell, a 2-D array of strings would work for me.

我可以使用 ASP.NET 3.5 中可用的任何内容,以及流行的 Json.NET(我已经将其用于客户端的序列化).

I can use anything available in ASP.NET 3.5, as well as the popular Json.NET (which I'm already using for serialization to the client).

显然,这些 JSON 库都没有这种开箱即用的明显功能——它们完全专注于通过强契约进行基于反射的反序列化.

Apparently neither of these JSON libraries have this forehead-slapping obvious capability out of the box--they are totally focused on reflection-based deserialization via strong contracts.

有什么想法吗?

限制:

  1. 我不想实现自己的 JSON 解析器
  2. 还不能使用 ASP.NET 4.0
  3. 希望远离旧的、已弃用的 JSON ASP.NET 类

推荐答案

Json.NET 做到了这一点...

string json = @"{""key1"":""value1"",""key2"":""value2""}";

var values = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);

更多示例:使用 Json.NET 序列化集合

这篇关于如何将 JSON 反序列化为一个简单的 Dictionary&lt;string,string&gt;在 ASP.NET 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆