简体   繁体   English

将 JSON 字符串转换为 C# 字典

[英]Convert JSON string to C# dictionary

I have a JSON string我有一个 JSON 字符串

{
  "Date":"21/11/2010"
  "name": "TEST"
  "place":"xyz"
}

I want to convert it into a C# dictionary without using a third party library我想在不使用第三方库的情况下将其转换为 C# 字典

You can do it natively since net 3.5 with jsonserializer. 你可以使用jsonserializer从net 3.5开始本机化。

var jss = new JavaScriptSerializer();
var dict = jss.Deserialize<Dictionary<string,string>>(jsonText);
var place = dict["place"]; // "xyz"

Here is a simple tutorial for your case: Quick JSON Serialization/Deserialization in C# 以下是针对您的案例的简单教程: C#中的快速JSON序列化/反序列化

Requires the System.Web.Extensions reference. 需要System.Web.Extensions引用。 If you can't find it, your program is probably using a Client target framework. 如果找不到,您的程序可能正在使用客户端目标框架。 Use a "Full" target framework. 使用“完整”目标框架。

You can now (for a while) use the inbuilt System.Text.Json for this as follows:您现在(暂时)可以为此使用内置的System.Text.Json ,如下所示:

var dict = JsonSerializer.Deserialize<Dictionary<string, string>>(jsonString);

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

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