C# json.net-无法解析嵌套对象
我试图从json字符串创建一个对象。 困难在于,对于json的一部分,我不知道键的编号或名称 以下是我到目前为止所做的尝试 json:C# json.net-无法解析嵌套对象,c#,json,json.net,C#,Json,Json.net,我试图从json字符串创建一个对象。 困难在于,对于json的一部分,我不知道键的编号或名称 以下是我到目前为止所做的尝试 json: { "browser":"Chrome", "call":{ "addEventListener":199, "appendChild":34, "createElement":8, "getAttribute":2170, }, "get":{ "linkColor":1,
{
"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> { }
公共类ClearCoatDataObject
{
公共字符串浏览器{get;set;}
公共字符串会话{get;set;}
公共字符串url{get;set;}
公共字符串tmsp{get;set;}
公共字符串_id{get;set;}
公共对象属性字典创建{get;set;}
公共对象属性字典调用{get;set;}
public ObjectProperties Dictionary get{get;set;}
公共对象属性字典集{get;set;}
公共静态ClearCoatDataObject FromJson(字符串json)
{
返回JsonConvert.DeserializeObject(json);
}
公共字符串ToJson()
{
返回JsonConvert.SerializeObject(this);
}
public ActionObjectsDictionary GetActions()
{
ActionObjectsDictionary actionTypes=新ActionObjectsDictionary();
添加(“创建”,创建);
添加(“调用”,调用);
actionTypes.Add(“get”,get);
actionTypes.Add(“set”,set);
返回操作类型;
}
公共ClearcoatDataItemCollection扁平化()
{
ClearcoatDataItemCollection retCollection=新的ClearcoatDataItemCollection();
//foreach constr在运行
ActionObjectsDictionary actionTypes=GetActions();
foreach(actionTypes中的KeyValuePair actionType)
{
objectProperties字典objectProperties=actionType.Value;
foreach(objectProperties中的KeyValuePair对象属性)
{
PropertyCountsDictionary propertyCounts=objectProperty.Value;
foreach(propertyCounts中的KeyValuePair propertyCount)
{
ClearCoatDataItem ccdi=新的ClearCoatDataItem(this.session、this.url、actionType.Key、objectProperty.Key、propertyCount.Key、propertyCount.Value);
retCollection.Add(ccdi);
}
}
}
退货回收;
}
}
//要容纳的词典类:
//行动(
//
公共类PropertyCountsDictionary:字典{}
公共类ObjectPropertiesDictionary:字典{}
公共类ActionObjectsDictionary:字典{}
当我运行代码时(这是针对Web服务的),我得到一个错误:
解析Json.Newtonsoft.Json.JsonSerializationException时出错:将值1转换为类型“PropertyCountsDictionary”时出错。路径“get.vlinkColor”,第1行,位置152。-->System.ArgumentException:无法从System.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> { }
}
公共类程序
{
私有静态void Main(字符串[]args)
{
var json=@”{
“浏览器”:“浏览器”,
“呼叫”:{
“addEventListener”:199,
“附加子女”:34,
“createElement”:8,
“getAttribute”:2170,
},
“获取”:{
“链接颜色”:1,
“vlinkColor”:1
},
“会话”:“3211658131”,
“tmStmp:”“21316503513854”
}";
var clearCoatDataObject=clearCoatDataObject.FromJson(json);
foreach(clearCoatDataObject.flatte()中的var ccdi)
{
WriteLine(ccdi.ToJson());
}
}
公共类ClearCoatDataObject
{
公共字符串浏览器{get;set;}
公共字符串会话{get;set;}
公共字符串url{get;set;}
公共字符串tmsp{get;set;}
公共字符串_id{get;set;}
公共属性字典创建{get;set;}
公共属性字典调用{get;set;}
公共属性dictionary get{get;set;}
公共属性字典集{get;set;}
公共静态ClearCoatDataObject FromJson(字符串json)
{
返回JsonConvert.DeserializeObject(json);
}
公共字符串ToJson()
{
返回JsonConvert.SerializeObject(this);
}
公共操作字典GetActions()
{
ActionDictionary actionTypes=新建ActionDictionary();
添加(“创建”,创建);
添加(“调用”,调用);
actionTypes.Add(“get”,get);
actionTypes.Add(“set”,set);
返回操作类型;
}
公共ClearcoatDataItemCollection扁平化()
{
ClearcoatDataItemCollection retCollection=新的ClearcoatDataItemCollection();
//foreach constr在运行
ActionDictionary actionTypes=GetActions();
foreach(actionTypes中的var actionType)
{
PropertyDictionary属性=actionType.Value;
if(属性!=null)
{
foreach(KeyValuePair属性Count in属性)
{
ClearCoatDataItem ccdi=新的ClearCoatDataItem(this.session,this.url,actionType.Key,propertyCount.Key,propertyCount.Value);
retCollection.Add(ccdi);
}
}
}
退货回收;
}
}
//要容纳的词典类:
//行动(
//
公共类属性字典:字典{}
公共类ActionDictionary:字典{}
}
谢谢,tmack。这是个愚蠢的错误。另外,我需要将字典从改为。int是