asp.net web服务:转换json对象
本文关键字:转换 json 对象 服务 net web asp | 更新日期: 2023-09-27 18:28:07
我在c#项目的参数中收到了这种类型的json:
{ "results": [
{
"FormID": 1,
"GateLetter": "B",
"ID": 1,
"FINNumber": "757",
"GateNumber": "345",
"FlightNumber": "1234",
"ShiftID": 1,
"LSAName": "",
"AirlineCode": "QK",
"LSAEmpID": "Another",
"FormName": "DFW Loader",
"createdAt": "2012-01-17T17:40:11.533Z",
"updatedAt": "2012-01-17T17:40:11.533Z",
"objectId": "sJS5iVXPot"
},
{
"FormID": 1,
"GateLetter": "",
"ID": 1,
"FINNumber": "",
"GateNumber": "",
"FlightNumber": "123",
"ShiftID": 1,
"LSAName": "",
"AirlineCode": null,
"LSAEmpID": "",
"FormName": "DFW Position",
"createdAt": "2012-01-17T20:58:17.932Z",
"updatedAt": "2012-01-17T20:58:17.932Z",
"objectId": "Ni8KspVjwA"
},
{
"FormID": 1,
"GateLetter": "a",
"ID": 1,
"FINNumber": "",
"GateNumber": "12",
"FlightNumber": "123",
"ShiftID": 1,
"LSAName": "",
"AirlineCode": "AC",
"LSAEmpID": "trent",
"FormName": "DFW Position",
"createdAt": "2012-01-17T23:31:11.686Z",
"updatedAt": "2012-01-17T23:31:11.686Z",
"objectId": "FXciW6zM6Q"
}
但它不是字符串,我的问题是:如何将其传输到字符串或var中能够反序列化它的后记。。。
转换为这种类型:
string data = @"[{""ShiftID"":""2"",""EmpName"":""dsdsfs""},{""ShiftID"":""4"",""EmpName"":""dsdsfd""}]";
这个类型可以完全反序列化,但不是第一个!
我相信你正在寻找的实际上是第三方:JSON.NET。我不知道你直接提到的这个翻译,但它将处理反序列化到你的POCO的中的问题
是的,第一个可以很好地反序列化:
public class FlightInfo
{
public string FormID {get;set;}
public string GateLetter {get;set;}
//...
public string objectId {get;set;}
}
public class FlightContainer
{
public List<FlightInfo> results;
}
JavaScriptSerializer js = new JavaScriptSerializer();
var deserializedObject = js.Deserialize<FlightContainer>(yourJsonString);
现在您可以访问您的FlightInfo元素,如下所示:
FlightInfo firstItem= deserializedObject.results[0];// by index
或者你可以迭代它们
foreach(var item in deserializedObject.results)
{
Console.WriteLine(item.FormID);
}