将 json 字符串转换为 c# 对象
本教程将演示如何使用两种不同的方法将 json 字符串转换为 c# 对象。
第一个使用 newtonsoft.json
库,而第二个使用 javascriptserializer
。这两种方法相似,但使用的语法略有不同,并且需要不同的引用。
使用 newtonsoft.json
将 json 字符串转换为 c# 对象
newtonsoft.json
库是 .net
的常用 json 框架。在将软件包安装到你的ag捕鱼王app官网的解决方案后,可以使用此库。你可以使用带有以下命令的 .net cli 来安装软件包。
> dotnet add package newtonsoft.json --version 13.0.1
json 字符串可以转换为任何类型的 c# 对象,只要它是对象类型的有效 json 字符串即可。这是通过反序列化字符串、将其转换为指定类型 (t) 并提供输入 json 字符串来完成的。
jsonconvert.deserializeobject(json_string);
使用 javascriptserializer 将 json 字符串转换为 c# 对象
将 json 字符串转换为 c# 对象的旧选项是 javascriptserializer
。虽然它没有 newtonsoft.json
ag捕鱼王app官网的解决方案那么快,但它仍然可以很好地利用。要使用此方法,你需要在项目中添加对 system.web.extensions.dll
的引用。
要添加该引用,请按照以下步骤操作。
- 导航到ag捕鱼王app官网的解决方案资源管理器
- 右键单击引用
-
点击
添加引用
-
单击左侧的
assemblies
选项卡 -
找到
system.web.extensions.dll
文件并单击ok
。
javascriptserializer
的工作方式与 newtonsoft.json
方法类似,因为它只需要 json 字符串和要反序列化的类型。
javascriptserializer().deserialize(json_string)
将 json 字符串转换为 c# 对象代码示例
下面的代码示例演示了如何使用 jsonconvert.deserializeobject
和 javascriptserializer
函数将 json 字符串转换为任何类型的 c# 对象。对于此示例,我们将字符串转换为包含整数的列表对象 (list
) 以及我们在程序中指定的自定义类。
using newtonsoft.json;
using system.data;
using system;
using system.componentmodel;
using system.collections.generic;
using system.web.script.serialization;
namespace json_to_object_example {
class program {
static void main(string[] args) {
// declare the json strings
string list_str = "[1, 2, 3, 9, 8, 7]";
string custom_class_str =
"{ \"name\":\"john doe\",\"age\":21,\"birthday\":\"2000-01-01t00:00:00\"}";
// ----- newtonsoft method -----
// deserialize the string by specifying the object type
list list_converted_ns = jsonconvert.deserializeobject>(list_str);
// print the contents of the newly converted list object to the console.
console.writeline("converted list - newtonsoft: " string.join(", ", list_converted_ns)
"\n");
// the same can be done for any type of c# object, including custom classes. just change the
// specified type.
custom_class class_converted_ns =
jsonconvert.deserializeobject(custom_class_str);
console.writeline("converted class - newtonsoft:");
printpropreties(class_converted_ns);
console.writeline("\n\n");
// ----- javascriptserializer method -----
// deserialize the string by specifying the object type
list list_converted_js = new javascriptserializer().deserialize>(list_str);
console.writeline(
"converted list - javascriptserializer: " string.join(", ", list_converted_js) "\n");
custom_class class_converted_js =
new javascriptserializer().deserialize(custom_class_str);
console.writeline("converted class - javascriptserializer:");
printpropreties(class_converted_js);
console.readline();
}
public class custom_class {
// custom class created for the purposes of this tutorial
public string name { get; set; }
public int age { get; set; }
public datetime birthday { get; set; }
}
public static void printpropreties(object obj) {
// uses the system.component model to read each property of a class and print its value
foreach (propertydescriptor descriptor in typedescriptor.getproperties(obj)) {
string name = descriptor.name;
object value = descriptor.getvalue(obj);
console.writeline("{0}: {1}", name, value);
}
}
}
}
此代码将输出由逗号连接的整数列表以及转换后的自定义类中的所有属性。你可以观察到两种方法都会产生相同的输出。
输出:
converted list - newtonsoft: 1, 2, 3, 9, 8, 7
converted class - newtonsoft:
name: john doe
age: 21
birthday: 01/01/2000 12:00:00 am
converted list - javascriptserializer: 1, 2, 3, 9, 8, 7
converted class - javascriptserializer:
name: john doe
age: 21
birthday: 01/01/2000 12:00:00 am
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
发布时间:2024/03/16 浏览次数:198 分类:编程语言
-
在 c# 中,有两种主要方法可用于将 list
转换为字符串变量,linq 方法和 string.join()函数。
发布时间:2024/03/16 浏览次数:171 分类:编程语言
-
在 c# 中,有两种主要方法可用于将 list
转换为字符串变量,linq 方法和 string.join()函数。
发布时间:2024/03/16 浏览次数:187 分类:编程语言
-
在 c# 中,有两种主要方法可用于将 list
转换为字符串变量,linq 方法和 string.join()函数。
在 c# 中发出 http post web 请求
发布时间:2024/02/04 浏览次数:131 分类:编程语言
-
在 c# 中,可以使用 3 种主要方法来发出 http post web 请求:webclient 类,httpwebrequest 类和 httpclient 类。本教程将讨论在 c# 中发出 http post web 请求的方法。使用 c# 中的 webclient 类发出 http post web 请求
发布时间:2024/02/04 浏览次数:130 分类:编程语言
-
process 类可用于在 c# 中运行命令提示符命令。在 c# 中使用 process.start() 函数运行命令提示符命令
发布时间:2024/02/04 浏览次数:203 分类:编程语言
-
有两种主要方法可用于在 c# 中调整图像的大小,bitmap 类构造函数和 graphics.drawimage()函数。在本教程中,我们将讨论在c#中调整图像大小的方法。我们将带您完成整个过程,从加载原始图像到保
发布时间:2024/02/04 浏览次数:138 分类:编程语言
-
有 3 种主要方法可用于下载 c# 中的图片,webclient.downloadfile()函数,bitmap 类和 image.fromstream()函数。在 c# 中使用 webclient 类下载图片 webclient 类提供了用于向 c# 中的 url 发送数据和从 url 接收数据
发布时间:2024/02/04 浏览次数:139 分类:编程语言
-
我们可以使用 stopwatch 类来计算 c# 中的经过时间。使用 c# 中的秒表类计算经过时间 stopwatch 类在 c# 中准确测量经过的时间。
发布时间:2024/02/04 浏览次数:200 分类:编程语言
-
有 3 种主要方法可用于获取 c# 中程序的可执行路径,即 assembly 类,appdomain 类和 path 类。本教程将介绍获取 c# 代码的可执行路径的方法。使用 c# 中的 assembly 类获取可执行路径