MonsterMMORPG MonsterMMORPG - 1 year ago 169
C# Question

How to compose dynamic expression from string

Here's the sample code

string srJson = File.ReadAllText("json.txt");

JObject rss = JObject.Parse(srJson);

dynamic json = JsonConvert.DeserializeObject(srJson);

foreach (var builds in json.result)
string fileName = builds.CommentId;

I am using Json.Net library. It uses dynamic expressions

How can i compose this CommentId from string ?

Currently i hard typed it but i want to be able to compose it from string so i can pull data from SQL server and get values i need.

C# .net 4.5 wpf

enter image description here

Answer Source

JObject supports IDictionary<string, object> interface. Instead of builds.CommentId you can use builds["COmmentId"].

There're ways to interact with dynamic types that don't implement dictionary interface, but they are complex and are not needed here to work with JSON.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download