Reputation: 165
I have JSON string in following format.
{
"Request": {
"Header": { "Action": "Login" },
"DataPayload": {
"UserName": "user",
"Password": "password"
}
}
}
I need to deserialize the above JSON string without creating any Type or Anonymous type and I should be able to access properties like below in .NET.
Request.Header.Action : To get action value.
Request.DataPayload.UserName : To get username.
Upvotes: 1
Views: 138
Reputation: 68710
You can do this easily using Json.NET.
Either parse your string as a JObject
and use it like a dictionary:
var obj = JObject.Parse(str);
var action = obj["Request"]["Header"]["Action"];
Or deserialize it into a dynamic object, if you don't mind losing static typing:
dynamic obj = JsonConvert.DeserializeObject<dynamic>(str);
var action = obj.Request.Header.Action;
Upvotes: 3