我试图从json字符串创建一个对象。困难在于,对于json的一部分,我将不知道密钥的数量或名称。
这是我到目前为止尝试过的。
json:{
"browser":"Chrome",
"call":{
"addEventListener":199,
"appendChild":34,
"createElement":8,
"getAttribute":2170,
},
"get":{
"linkColor":1,
"vlinkColor":1
},
"session":"3211658131",
"tmStmp":"21316503513854"
}
对象:
public class ClearCoatDataObject
{
public string browser { get; set; }
public string session { get; set; }
public string url { get; set; }
public string tmStmp { get; set; }
public string _id { get; set; }
public ObjectPropertiesDictionary create { get; set; }
public ObjectPropertiesDictionary call { get; set; }
public ObjectPropertiesDictionary get { get; set; }
public ObjectPropertiesDictionary set { get; set; }
public static ClearCoatDataObject FromJson(string json)
{
return JsonConvert.DeserializeObject<ClearCoatDataObject>(json);
}
public String ToJson()
{
return JsonConvert.SerializeObject(this);
}
public ActionObjectsDictionary GetActions()
{
ActionObjectsDictionary actionTypes = new ActionObjectsDictionary();
actionTypes.Add("create", create);
actionTypes.Add("call", call);
actionTypes.Add("get", get);
actionTypes.Add("set", set);
return actionTypes;
}
public ClearcoatDataItemCollection Flatten()
{
ClearcoatDataItemCollection retCollection = new ClearcoatDataItemCollection();
// foreach constr in action
ActionObjectsDictionary actionTypes = GetActions();
foreach (KeyValuePair<string, ObjectPropertiesDictionary> actionType in actionTypes)
{
ObjectPropertiesDictionary objectProperties = actionType.Value;
foreach (KeyValuePair<string, PropertyCountsDictionary> objectProperty in objectProperties)
{
PropertyCountsDictionary propertyCounts = objectProperty.Value;
foreach (KeyValuePair<string, int> propertyCount in propertyCounts)
{
ClearCoatDataItem ccdi = new ClearCoatDataItem(this.session, this.url, actionType.Key, objectProperty.Key, propertyCount.Key, propertyCount.Value);
retCollection.Add(ccdi);
}
}
}
return retCollection;
}
}
// Dictionary Classes to hold:
// actions (
//
public class PropertyCountsDictionary : Dictionary<string, int> { }
public class ObjectPropertiesDictionary : Dictionary<string, PropertyCountsDictionary> { }
public class ActionObjectsDictionary : Dictionary<string, ObjectPropertiesDictionary> { }
当我运行代码(这是一个webservice),我得到一个错误:解析Json时出错。json . jsonserializationexception:将值1转换为"PropertyCountsDictionary"类型错误。路径"。vlinkColor',第一行,位置152。——>系统。无法从系统强制转换或转换。Int64到PropertyCountsDictionary.
谢谢你能提供的任何帮助。
看起来你有一个额外的字典,你不需要。这就是你想要的吗?
public class Program
{
private static void Main(string[] args)
{
var json = @"{
""browser"":""Chrome"",
""call"":{
""addEventListener"":199,
""appendChild"":34,
""createElement"":8,
""getAttribute"":2170,
},
""get"":{
""linkColor"":1,
""vlinkColor"":1
},
""session"":""3211658131"",
""tmStmp"":""21316503513854""
}";
var clearCoatDataObject = ClearCoatDataObject.FromJson(json);
foreach (var ccdi in clearCoatDataObject.Flatten())
{
Console.WriteLine(ccdi.ToJson());
}
}
public class ClearCoatDataObject
{
public string browser { get; set; }
public string session { get; set; }
public string url { get; set; }
public string tmStmp { get; set; }
public string _id { get; set; }
public PropertyDictionary create { get; set; }
public PropertyDictionary call { get; set; }
public PropertyDictionary get { get; set; }
public PropertyDictionary set { get; set; }
public static ClearCoatDataObject FromJson(string json)
{
return JsonConvert.DeserializeObject<ClearCoatDataObject>(json);
}
public String ToJson()
{
return JsonConvert.SerializeObject(this);
}
public ActionDictionary GetActions()
{
ActionDictionary actionTypes = new ActionDictionary();
actionTypes.Add("create", create);
actionTypes.Add("call", call);
actionTypes.Add("get", get);
actionTypes.Add("set", set);
return actionTypes;
}
public ClearcoatDataItemCollection Flatten()
{
ClearcoatDataItemCollection retCollection = new ClearcoatDataItemCollection();
// foreach constr in action
ActionDictionary actionTypes = GetActions();
foreach (var actionType in actionTypes)
{
PropertyDictionary property = actionType.Value;
if (property != null)
{
foreach (KeyValuePair<string, int> propertyCount in property)
{
ClearCoatDataItem ccdi = new ClearCoatDataItem(this.session, this.url, actionType.Key, propertyCount.Key, propertyCount.Value);
retCollection.Add(ccdi);
}
}
}
return retCollection;
}
}
// Dictionary Classes to hold:
// actions (
//
public class PropertyDictionary : Dictionary<string, int> { }
public class ActionDictionary : Dictionary<string, PropertyDictionary> { }
}