【发布时间】:2016-09-17 02:02:23
【问题描述】:
最初我将 JSON 字符串反序列化为动态类型列表,这工作正常,直到将其移动到服务器,此时动态列表停止工作。我能找到的唯一答案是,这不是列表的异常行为。 The original question
因此,我将动态列表更改为 ExpandoObject
dynamic root = JsonConvert.DeserializeObject<ExpandoObject>(jsonstring, new ExpandoObjectConverter());
最初在我调用的动态列表中
root.record.form_values.Remove("f161");
root.record.form_values.Remove("41df");
root.record.form_values.Remove("a228");
root.record.Remove("changeset_id");
按预期从列表中删除了这些对象,我最终将其转换回 JSON 并将其发送回 API。
在阅读了 ExpandoObjects 之后,我发现要删除它,您需要将其扔到一个 IDictionary 中才能获得 .Remove 功能。
所以我这样做了:
dynamic dict = (IDictionary<string, object>)root;
然后将我的代码替换为:
dict.record.form_values.Remove("f161");
dict.record.form_values.Remove("41df");
dict.record.form_values.Remove("a228");
dict.record.Remove("changeset_id");
这导致了错误
'System.Dynamic.ExpandoObject' 不包含对 '删除'
所以在阅读了更多内容后,我尝试了以下方法:
dict = (IDictionary<string, object>)root.record.form_values.Remove("f161");
dict = (IDictionary<string, object>)root.record.form_values.Remove("41df");
dict = (IDictionary<string, object>)root.record.form_values.Remove("a228");
dict = (IDictionary<string, object>)root.record.Remove("changeset_id");
导致同样的错误...
如果有人能告诉我从现在到哪里去,那就太好了。
仅供参考 JSON 结构如下所示:
"{
\"record\":{
\"status\":\"somevalue\",
\"form_values\":
{
\"833b\":\"somevalue\",
\"683b\":\"somevalue\",
\"c9ca\":{\"other_values\":[],\"choice_values\":[\"somevalue\"]}
},
\"latitude\":somevalue,
\"longitude\":somevalue
}
}"
DynamicList 的原始代码(有效)如下所示:
string jsonstring = data;
var root = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(jsonstring);
root.record.assigned_to = assignedto;
root.record.assigned_to_id = assignedtoid;
root.record.status = status.ToString();
root.record.bb42 = abudgetunit;
root.record.f694 = abudgetunitstr;
root.record.form_values.Remove("f161");
root.record.form_values.Remove("41df");
root.record.form_values.Remove("a228");
root.record.Remove("changeset_id");
【问题讨论】: