我有一个这样的Json:
{
"myClass1" : [
{
"field1" : "value",
"field2" : "value",
"field3" : "value"
},
{
"field1" : "value",
"field2" : "value",
"field3" : "value"
},
{
"field1" : "value",
"field2" : "value",
"field3" : "value"
}
],
"myClass2" : {
"1" : [
{
"field1" : "value",
"field2" : "value",
"field3" : "value"
},
{
"field1" : "value",
"field2" : "value",
"field3" : "value"
},
{
"field1" : "value",
"field2" : "value",
"field3" : "value"
}
],
"2" : [
{
"field1" : "value",
"field2" : "value",
"field3" : "value"
},
{
"field1" : "value",
"field2" : "value",
"field3" : "value"
},
{
"field1" : "value",
"field2" : "value",
"field3" : "value"
}
],
"3" : [
{
"field1" : "value",
"field2" : "value",
"field3" : "value"
},
{
"field1" : "value",
"field2" : "value",
"field3" : "value"
},
{
"field1" : "value",
"field2" : "value",
"field3" : "value"
}
]
}
}
课程:
class myClass1
{
public string field1 { get; set; }
public string field2 { get; set; }
public string field3 { get; set; }
}
class myClass2
{
public string field1 { get; set; }
public string field2 { get; set; }
public string field3 { get; set; }
}
class Parser
{
public myClass1[] object1 { get; set; }
public myClass2[,] object2 { get; set; }
}
使用NewtonSoft Json解析器,我无法解析第二个类(第一个我没有问题!)我尝试myClass2 [](显然是不正确的)myClass2 [,]和myClass2 [] []并没有任何效果 . 我做错了什么?我有错误:
发生了Newtonsoft.Json.JsonSerializationException HResult = 0x80131500 Message =无法将当前JSON对象(例如{“name”:“value”})反序列化为类型'CreativaTV.Soporte [] []',因为该类型需要JSON数组(例如[1,2,3])正确反序列化 . 要修复此错误,请将JSON更改为JSON数组(例如[1,2,3])或更改反序列化类型,使其成为普通的.NET类型(例如,不是像整数这样的基本类型,而不是类似的集合类型可以从JSON对象反序列化的数组或List . JsonObjectAttribute也可以添加到类型中以强制它从JSON对象反序列化 . 路径'soportes.2',第185行,第12位.Source = Newtonsoft.Json StackTrace:at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader,Type objectType,JsonContract contract,JsonProperty member,JsonContainerContract containerContract,JsonProperty containerMember,Object ExistingValue)在Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader,Type objectType,JsonContract contract,JsonProperty成员,JsonContainerContract containerContract,JsonProperty containerMember,Object existingValue)at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty属性,JsonConverter propertyConverter) ,JsonContainerContract containerContract,JsonProperty containerProperty,JsonReader reader,Object target)at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject,JsonReader reader,JsonObjectContract contract,JsonProperty member,String id)at at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader,Type objectType,JsonContract contract,JsonProperty成员,JsonContainerContract containerContract,JsonProperty containerMember,Object existingValue)at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader,Type objectType,JsonContract contract) Newtonoft.Json上的Newtonoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader,Type objectType)中的Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader,Type objectType,Boolean checkAdditionalContent),JsonProperty成员,JsonContainerContract containerContract,JsonProperty containerMember,Object existingValue)在Newtonsoft.Json.JsonConvert.DeserializeObject [T](String val)的Newtonsoft.Json.JsonConvert.DeserializeObject [T](字符串值,JsonSerializerSettings设置)的.Json.JsonConvert.DeserializeObject(字符串值,类型类型,JsonSerializerSettings设置) ue)在C:\ AppDev \ CreativaTV \ CreativaTV \ CreativaTV \ Form1.cs中的CreativaTV.Form1.CargarContenido():位于C:\ AppDev \ CreativaTV \ CreativaTV的Creativa.Form1.Form1_Load(对象发送者,EventArgs e)的第61行\ CreativaTV \ Form1.cs:位于System.Windows.Forms.Form.OnCoateControl()的System.Windows.Forms.Form.OnLoad(EventArgs e)的第32行,位于System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)at系统.Windows.Forms.Conm.WreSCon的循环中的System.Windows.Forms.Conm.WndProc(Message&m)上的System.Windows.Forms.Control.CreateControl()处于System.Windows.Forms.Conm . 消息&m)System.Windows.Forms.Form.WndProc(Message&m)的System.Windows.Forms.Form.WmShowWindow(Message&m)位于System的System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m) . System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)中的Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)
2 回答
看起来应该是:
虽然看起来
Dictionary<int, myClass2[]>
也有效 .同样的问题一遍又一遍得到相同的答案,但我只是复制并粘贴“同一个问题”中的答案:
“在Json规范中,您可以看到
[
表示json对象的开始数组,而{
表示新json对象的开始 . ”现在,如果你看看你的JSON,你可以看到这些
这应该会让你知道什么是错的 .
这两个都是对象数组,但在你的第二个"class"中,在实际数组之前有一个"key" . 所以要解决这个问题,你应该将"model"从这个
public myClass2[,] object2 { get; set; }
改为这样的东西:public KeyValuePair<string, myClass[]>[] meClass2 {get;set;}
或public Dictionary<string, myClass2[]> meClass2 {get;set;}
.相关(类似)问题:
Unexpected character encountered while parsing value: j. Path '', line 0, position 0
How to parse a json array json.net