如何将 JSON 映射到 C# 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9988395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to map JSON to C# Objects
提问by user710502
I am having issues with understanding how to make this happen.
我在理解如何实现这一点时遇到问题。
Basically we have an API, the user sends a JSON of the format: (excuse the code if not perfect but you understand)
基本上我们有一个 API,用户发送一个 JSON 格式:(如果代码不完美,但你理解,请原谅)
{"Profile": [{
"Name":"Joe",
"Last :"Doe",
"Client":
{
"ClientId":"1",
"Product":"Apple",
"Message":"Peter likes apples"
},
"Date":"2012-02-14",
}]}
Ok I'm not sure if I got that JSON formatted correctly but now here is my issue.
好的,我不确定我的 JSON 格式是否正确,但现在这是我的问题。
I have a class called Profilewith parameters Name, Last, and an object as one of its members called Client as well as property Date.
我有一个类Profile,其参数为 Name、Last 和一个对象作为其成员之一,称为 Client 以及属性 Date。
Something like this:
像这样的东西:
public class Profile
{
public string Name {get; set;}
public string Last {get; set;}
public Client client {get; set;}
public DateTime dDate {get; set;}
}
So basically, I am not sure how to grab the JSON and then map it to my object.
所以基本上,我不确定如何获取 JSON 然后将其映射到我的对象。
Any help with "helping" me understand would be much appreciated.
任何“帮助”我理解的帮助将不胜感激。
采纳答案by L.B
You can use Json.NETto deserialize your json string as (with some modifications to your classes)
您可以使用Json.NET将您的 json 字符串反序列化为(对您的类进行一些修改)
var yourObject = JsonConvert.DeserializeObject<Root>(jsonstring);
public class Root
{
public Profile[] Profile;
}
public class Profile
{
public string Name { get; set; }
public string Last { get; set; }
public Client Client { get; set; }
public DateTime Date { get; set; }
}
public class Client
{
public int ClientId;
public string Product;
public string Message;
}
回答by Marek
You can use a JSON library for this, for example Newtonsoft.Jsonwhich is free. It will map json to your types automatically.
您可以为此使用 JSON 库,例如免费的Newtonsoft.Json。它会自动将 json 映射到您的类型。
Sample:
样本:
public static T Deserialize<T>(string json)
{
Newtonsoft.Json.JsonSerializer s = new JsonSerializer();
return s.Deserialize<T>(new JsonTextReader(new StringReader(json)));
}
There is also a NuGet package available.
还有一个 NuGet 包可用。
回答by lazyGoose
DataContractJsonSerializerdoes the job, but it uses more sophisticated format for DateTime serialization.
DataContractJsonSerializer 可以完成这项工作,但它使用更复杂的格式进行 DateTime 序列化。
回答by Buzzzzzzz
Easiest way I know is to use JSON.Net by newtonsoft. To make it easier to understand, I always make matching classes in C# with the same name. Then its easier to deserialize it. As an example, if it is an array of objects in js, it will map to a list of object with the same names in C#. As for the date time, its a bit tricky. Id do the client side validation and Datetime.tryParse in the serverside, itll take care of the dashes or slashes.
我知道的最简单的方法是使用 newtonsoft 的 JSON.Net。为了更容易理解,我总是在 C# 中使用相同的名称匹配类。然后它更容易反序列化它。举个例子,如果是js中的一个对象数组,它会映射到C#中同名的对象列表。至于日期时间,它有点棘手。Id 在服务器端进行客户端验证和 Datetime.tryParse,它会处理破折号或斜线。
var serializer = new JavaScriptSerializer();
List<abc> abcList = serializer.Deserialize<List<abc>>(PassedInJsonString);

