C#中的Json反序列化 [英] Json deserialization in c#
本文介绍了C#中的Json反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在获取嵌套多维数组数据的动态JSON数据形式,因此我无法定义类和设置属性,现在如何在c#中以适当的格式获取数据的所有键和值.
Hi,
I am getting dynamic json data form of nested multidimensional array data is dynamic so i cant define class and set the properties, now how can i get all keys and values of data in a proper format in c#.
推荐答案
使用 fastJSON [
Use fastJSON[^]''sParse()
method it will give youDictionary<string,object>
representation of the JSON which you can traverse.
JavaScriptSerializer,这是.net框架中的内置类
JavaScriptSerializer this is inbuilt class in .net framework
using System.Web.Script.Serialization;
public void Deserialize()
{
JavaScriptSerializer js = new JavaScriptSerializer();
string jScript = "{NAME:'SAMPLE', AGE:'00',INTEREST:'CODEPROJECT'}";
EmployeeRecord emp = js.Deserialize<employeerecord>(jScript);
}
public class EmployeeRecord
{
public string Name { get; set; }
public int Age { get; set; }
public string Interest { get; set; }
}
如果JSON位于数组中,请使用list转换为c#对象
In case JSON is in array, use list to convert to c# objects
string jScript = "[{NAME:'SAMPLE', AGE:'00',INTEREST:'CODEPROJECT'},{NAME:'SAMPLE1', AGE:'01',INTEREST:'CODEPROJECT1'}]";
List< EmployeeRecord > sc = js.Deserialize<List< EmployeeRecord >>(jScript);
有关更多信息,请 MSDN
for more information MSDN
这篇关于C#中的Json反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文