.Net SimpleJson: JSON deserialization for a dynamic object

I am using the SimpleJson library from here: http://simplejson.codeplex.com/

I would like to deserialize the JSON string for a dynamic object as follows:

dynamic json = SimpleJson.SimpleJson.DeserializeObject("{\"foo\":\"bar\"}"); var test = json.foo; 

Part of deserialization works correctly, but calling json.foo raises a RuntimeBinderException with the error message 'SimpleJson.JsonObject' does not contain a definition for 'foo' .

How can I deserialize a JSON string using SimpleJson and access dynamic properties using json.foo syntax?

+6
source share
3 answers

Well, it's just a matter of reading the source code for SimpleJson. :-) The line must be uncommented to support the dynamic syntax I'm looking for. Not sure why this is not enabled by default.

From the source code:

 // NOTE: uncomment the following line to enable dynamic support. //#define SIMPLE_JSON_DYNAMIC 
+9
source

When looking at samples, JsonObject properties JsonObject available as a dictionary. So instead of json.foo you will need json["foo"] .

Actually, it’s worse for you to use dynamic here, since there is nothing dynamic in it: the method returns a JsonObject , which simply does not have a foo member. If you did not use dynamic , you might get this error message at compile time.

If you look at the LB link, provided that it shows how to implement this dynamic functionality yourself.

+2
source

csc / t: library / d: SIMPLE_JSON_DYNAMIC SimpleJson.cs

+1
source

Source: https://habr.com/ru/post/899813/


All Articles