0

So I've got a JSON string that I'm handling with json.net. I'm putting this into a dynamic object and then just accessing the values from that.

dynamic data = JObject.Parse(JsonString);

if(data !=null)
{
    string names = data.names.all
    int count = data.member_count;
}

The issue is if data doesn't contain member_count (or names.all) it doesn't throw an error it just stops dead (I assume it returns out). I've tried to search for a solution but I'm not even sure how to word it.

5
  • Firstly, why are you using dynamic? Secondly, why don't you deserialize this to a concrete class? Thirdly, you haven't stated what you want this to do and there is only a vague statement of what its doing, meaning you haven't debugged this... In all honesty i think you need to work on this question Commented Mar 23, 2020 at 0:25
  • I don't think there is no exception. It should throw RuntimeBinderException as the binder inside the dynamic impl can't figure out the property. You might have a try-catch somewhere made it look like silent exception. Commented Mar 23, 2020 at 0:47
  • @Michael Randall I'm using dynamic so I can easily access that parts of the json that I need. I can't use a concrete class as the json that is returned is not consistent. As for it being vague thats all the code thats needed - getting the value from the json into that (string/int), unless those values are not in the json in which case it just does nothing when it reaches it. Commented Mar 23, 2020 at 14:47
  • @weichch I would have thought so too but its not, it is running in an async task but I don't expect that to effect it. Its really odd (for me) that when it tries to access something that doesn't exsist it just 'stops' without flagging or throwing anything. Commented Mar 23, 2020 at 14:49
  • @cooltrain Ok that might explain it a bit further Did you await the task? Commented Mar 23, 2020 at 19:38

0

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.