My C# program will make a REST API call. The response message format is JSON (the converted soap XML message to JSON).
The nested JSON response message will be similar to the following:
{
"soap:Envelope": {
"@xmlns:soap": "http://schemas.xmlsoap.org/soap/envelope/",
"soap:Header": {
"omni:To": "http://www.w3.org/2005/08/tset/usr",
"omni:From": {
"omni:Address": "https://myweb.test/1UNIW"
},
"omni:Action": "http://webservices.apitest.com/1UNIW",
"omni:MessageID": "j00012",
"omnii:Session": {
"omnii:SecurityToken": "2S8BN3BPJMSHQK4OYKZRODFW3"
}
},
"soap:Body": {
"MyFunct": {
"@xmlns": "http://xml.test.com/1UNIW",
"replyInfo": {
"status": {
"tierTypeInfo": "IJK"
}
},
"FleetIndex": [
{
"groupOfObject": {
"propDetail": {},
"itemDetails": [
{
"ietmInformation": {
"itemDateTime": {
"dateOfDeparture": "270224"
},
"itemNumber": "2057"
}
}
]
}
}
],
"serviceCoverageInfoGrp": {
"itemNumberInfo": {
"itemNumber": {
"number": "1"
}
}
}
}
}
}
}
I tried to deseralize the JSON data using this code:
var v = JsonConvert.DeserializeObject<ResultResponse>(jsonmessage);
I have created ResultResponse
object starting from "FleetIndex"
. But the deserialization is failing.
I am not sure about the best solution to process the above JSON message. I appreciate your suggestion.
Thanks in advance for your help
2
Answers
As an option you can do next way:
It’s a lazy way, but should work.
You haven’t shared your ResultResponse data model, so you might need to check the depth in
root["soap:Envelope"]["soap:Body"]["MyFunct"]["FleetIndex"];
statement.One can deserialize XML or JSON to a "data class". If you already have the XML, it seems pointless to convert to JSON before deserializing. In either case, "copy" the XML or JSON and "Paste Special…" in Visual Studio to get the equivalent C# class. And there is no gain trying to do a "partial" deserialization; one deserializes the whole thing for simplicity and compile time validation when accessing the elements.