如何使用JsonConvert.DeserializeObject在C#中将数组转换为Model [英] How to ues JsonConvert.DeserializeObject to convert array to Model in C#
本文介绍了如何使用JsonConvert.DeserializeObject在C#中将数组转换为Model的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有我的数据.
如何将其转换为模型?
{"test": ["123","456"]}
推荐答案
如果您已经拥有json字符串并希望将其映射到C#类构造,则可以使用Intigted Visual Studio函数将Json粘贴为类.
If you already have a json string and want it to map it to a C# class construct you can use the intigrated Visual Studio function Paste Json as Classes.
- 复制一些JSON
- 选择编辑"->选择性粘贴"->将JSON作为类粘贴"
如果这样做,Visual Studio将为您提供此类:
If you do so Visual Studio will make for you this class:
public class Rootobject
{
public string[] test { get; set; }
}
旁注:
如果您不使用Visual Studio,则可以访问此站点.它将为您提供相同的功能并获得相似的结果.
Side note:
If you are not using Visual Studio you can go visit this site. Which will provide you the same feature with a similar result.
要反序列化,只需致电:
To deserialize you simply call:
var json = "{\"test\": [\"123\",\"456\"]}";
var myObject = JsonConvert.DeserializeObject<Rootobject>(json);
这篇关于如何使用JsonConvert.DeserializeObject在C#中将数组转换为Model的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文