0

I have a string value:

var responseString = {"ErrorType":"ServerError","Message":"Incoming data error.","Properties":null}

When I call JObject.Parse(responseString);, I get the following dynamic object:

{{  
"ErrorType": "ServerError",  
"Message": "Incoming data error.",  
"Properties": null
}}

Why is JObject creating a dynamic object that is an object wrapped in an object? I was hoping to write code to access the Message property such as responseMessage.Message as string, but that throws an error.

1 Answer 1

1

I just tried the following code in LinqPad:

var responseString = "{\"ErrorType\":\"ServerError\",\"Message\":\"Incoming data error.\",\"Properties\":null}";
dynamic responseMessage = JObject.Parse(responseString);
var msg = (string) responseMessage.Message;
msg.Dump();

In the output, I get the desired Incoming data error. string, so it looks like it is doing what it should. How does your code look? And what version of Json.NET are you using? Also, it is NOT possible to use as to convert to string, as this will return null, since the value is a JToken. You need the explicit cast.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the extra explanation... that helped narrow down my issues exactly. I was using as. And then when it wasn't working correctly, I tried to use VS's watch window to test different properties and those would give me the error message.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.