Periods in the name of c# dynamic ExpandoObjects?

时光毁灭记忆、已成空白 提交于 2019-12-02 04:27:15

问题


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!


回答1:


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.




回答2:


Using Json.Net

JObject jObj = new JObject();

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

var json = jObj.ToString();


来源:https://stackoverflow.com/questions/10019123/periods-in-the-name-of-c-sharp-dynamic-expandoobjects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!