Reputation: 11
i receive this type of json in parameter in my c# project :
{ "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"
}
But it is not a string, my question is : how can i transport this into a string or var to be able to deserialize it afterwords...
into this type :
string data = @"[{""ShiftID"":""2"",""EmpName"":""dsdsfs""},{""ShiftID"":""4"",""EmpName"":""dsdsfd""}]";
this type can be deserialize perfectly but not the first one!
Upvotes: 0
Views: 332
Reputation: 63956
Yes, the first one can be deserialized just fine:
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);
Now you can access your FlightInfo elements like so:
FlightInfo firstItem= deserializedObject.results[0];// by index
Or you can iterate through them
foreach(var item in deserializedObject.results)
{
Console.WriteLine(item.FormID);
}
Upvotes: 0
Reputation: 67075
I believe that what you are looking for is actually a third party: JSON.NET. I do not know about this translating as you directly mentioned, but it will deal with the deserialization into one of your POCO's
Upvotes: 1