我有一个json集合,它使用数字作为属性名称。我正在使用Newtonsoft JsonConvert尝试反序列化为c#对象,但它失败了。这甚至可能吗?
{
"0":{"_bnd":{"_path":"Style","_parts":["Style"],"_key":"Style"}},
"1":{"_bnd":{"_path":"AcctPerfAsOfDate","_parts":"AcctPerfAsOfDate"],"_key":"AcctPerfAsOfDate"}},"length":2,"_updating":0,"collectionChanged":{"_handlers":[{}]}
}
答案 0 :(得分:0)
{
"0":{"_bnd":{"_path":"Style","_parts":["Style"],"_key":"Style"}},
"1":{"_bnd":{"_path":"AcctPerfAsOfDate","_parts":"AcctPerfAsOfDate"],"_key":"AcctPerfAsOfDate"}},"length":2,"_updating":0,"collectionChanged":{"_handlers":[{}]}
Remove this ^
}
删除]以获取有效的JSON。
更新:
这是JSON的类结构:
public class Bnd
{
public string _path { get; set; }
public List<string> _parts { get; set; }
public string _key { get; set; }
}
public class __invalid_type__0
{
public Bnd _bnd { get; set; }
}
public class Bnd2
{
public string _path { get; set; }
public string _parts { get; set; }
public string _key { get; set; }
}
public class __invalid_type__1
{
public Bnd2 _bnd { get; set; }
}
public class Handler
{
}
public class CollectionChanged
{
public List<Handler> _handlers { get; set; }
}
public class RootObject
{
public __invalid_type__0 __invalid_name__0 { get; set; }
public __invalid_type__1 __invalid_name__1 { get; set; }
public int length { get; set; }
public int _updating { get; set; }
public CollectionChanged collectionChanged { get; set; }
}
答案 1 :(得分:0)
集合中的第二项是不明确的。不确定length,_updating和collectionChanged字段是属于第二个项目还是属于集合。 我删除它来创建示例,但您可以轻松更改匿名对象以反映您的需求。
void Main()
{
JsonConvert.DeserializeAnonymousType(U.InputText(), new Dictionary<int, object> { })
.ToDictionary(a=>a.Key,a=> ((JObject) a.Value).ToAnonymous(new { _bnd = new {_path=""}})).Dump();
}
public static class ext
{
public static T ToAnonymous<T>(this JObject source, T obj) => (T)source.ToObject(obj.GetType());
}