9

I'm trying to read values from System.Web.Helpers.DynamicJsonObject. I can see the values in the debugger but I can't figure out how to access them. I have tried this

item.GetType().GetProperty("batch_id").GetValue(item, null);

but when I try that I get this response in the debugger "item.GetType().GetProperty("batch_id")' is null"

I have attached a picture from my solution enter image description here

Thank you, -Tesh

tereško
  • 58,060
  • 25
  • 98
  • 150
MindGame
  • 1,211
  • 6
  • 29
  • 50

3 Answers3

32

It is dynamic so you can just do:

string batchId = item.batch_id;

If for some reason you have the property name in a string, and don't know it at compile time, the indexing operator will work:

string value = item["batch_id"];
driis
  • 161,458
  • 45
  • 265
  • 341
  • Thanks driss, that worked perfectly. I'm glad you mentioned the item["batch_id"], because that was my next question :). – MindGame Oct 16 '12 at 21:31
  • 1
    Please can you explain in which case property names would be in "string" and in which case as normal properties ? – Omer Javed Jan 28 '15 at 08:37
4

Try enumerating the values DynamicJsonObject.GetDynamicMemberNames Method. It returns an IEnumerable of string.

decyclone
  • 30,394
  • 6
  • 63
  • 80
1

It doesn't work because they are fields, not properties. And, yeah, it is dynamic, so you can use just item.batch_id.

Sergei B.
  • 3,227
  • 19
  • 18