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.

cooltrain
  • 31
  • 1
  • 7
  • 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 – TheGeneral Mar 23 '20 at 00: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. – weichch Mar 23 '20 at 00: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. – cooltrain Mar 23 '20 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. – cooltrain Mar 23 '20 at 14:49
  • @cooltrain Ok that might explain it a bit further Did you `await` the task? – weichch Mar 23 '20 at 19:38

0 Answers0