2

Maybe this is a silly question, but I'm working on a project that wants me to generate some JSON that looks like this:

{'action.type':'post', 'application':APP_ID}

In C#, I'm trying to create this "action.type" attribute, with the value of "post". How would I do that? Here's how I've typlically been creating stuff like:

dynamic ActionSpec = new ExpandoObject();
ActionSpec.SomeParam = "something";
ActionSpec.id = 12345;

I can't go "ActionSpec.action.type", because that will not output the desired "action.type". Does this make sense? Thanks!

Jim Aho
  • 9,932
  • 15
  • 56
  • 87
kevin
  • 417
  • 4
  • 20

2 Answers2

8

You could try populating it via the dictionary:

IDictionary<string, object> expandoDictionary = ActionSpec;
expandoDictionary["action.type"] = "post";

However, I wouldn't be at all surprised if it rejected that as an invalid identifier.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

Using Json.Net

JObject jObj = new JObject();

jObj["action.type"] = "post";
jObj["application"] = "APP_ID";

var json = jObj.ToString();
L.B
  • 114,136
  • 19
  • 178
  • 224