无法解析Json

时间:2017-05-17 14:49:21

标签: c# json serialization json.net

我有一个像这样的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   对象(例如{"名称":"值"})进入类型' CreativaTV.Soporte [] []'   因为该类型需要一个JSON数组(例如[1,2,3])来反序列化   正确。要修复此错误,请将JSON更改为JSON数组   (例如[1,2,3])或更改反序列化类型以使其正常   .NET类型(例如,不是整数的基本类型,不是集合   类似于数组或List的类型,可以从JSON反序列化   宾语。 JsonObjectAttribute也可以添加到类型中以强制它   从JSON对象反序列化。路径' soportes.2',第185行,   位置12.来源= Newtonsoft.Json StackTrace:at   Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader   reader,Type objectType,JsonContract契约,JsonProperty成员,   JsonContainerContract containerContract,JsonProperty containerMember,   对象existingValue)at   Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader   reader,Type objectType,JsonContract契约,JsonProperty成员,   JsonContainerContract containerContract,JsonProperty containerMember,   对象existingValue)at   Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty   property,JsonConverter propertyConverter,JsonContainerContract   containerContract,JsonProperty containerProperty,JsonReader reader,   对象目标)   Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(对象   newObject,JsonReader reader,JsonObjectContract契约,   JsonProperty成员,String id)at   Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader   reader,Type objectType,JsonContract契约,JsonProperty成员,   JsonContainerContract containerContract,JsonProperty containerMember,   对象existingValue)at   Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader   reader,Type objectType,JsonContract契约,JsonProperty成员,   JsonContainerContract containerContract,JsonProperty containerMember,   对象existingValue)at   Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader   reader,Type objectType,Boolean checkAdditionalContent)at   Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader阅读器,   输入objectType)at   Newtonsoft.Json.JsonConvert.DeserializeObject(字符串值,类型类型,   JsonSerializerSettings设置)at   Newtonsoft.Json.JsonConvert.DeserializeObject [T](字符串值,   JsonSerializerSettings设置)at   Newtonsoft.Json.JsonConvert.DeserializeObject [T](String value)at   CreativaTV.Form1.CargarContenido()in   C:\ AppDev \ CreativaTV \ CreativaTV \ CreativaTV \ Form1.cs:第61行   CreativaTV.Form1.Form1_Load(Object sender,EventArgs e)in   C:\ AppDev \ CreativaTV \ CreativaTV \ CreativaTV \ Form1.cs:第32行   System.Windows.Forms.Form.OnLoad(EventArgs e)at   System.Windows.Forms.Form.OnCreateControl()at   System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
  在System.Windows.Forms.Control.CreateControl()at   System.Windows.Forms.Control.WmShowWindow(Message& m)at   System.Windows.Forms.Control.WndProc(Message& m)at   System.Windows.Forms.ScrollableControl.WndProc(Message& m)at   System.Windows.Forms.Form.WmShowWindow(Message& m)at   System.Windows.Forms.Form.WndProc(Message& m)at   System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)   在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&   m)在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr)   hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)

2 个答案:

答案 0 :(得分:2)

看起来应该是:

public myClass1[] myClass1 { get; set; }
public Dictionary<string, myClass2[]> myClass2 { get; set; }

虽然似乎Dictionary<int, myClass2[]>也有效。

答案 1 :(得分:1)

同样的问题一遍又一遍得到相同的答案,但我只是复制并粘贴我的回答#34;同样的问题&#34; :

&#34;在Json规范中,您可以看到void MediaHandler::changeMedia(std::string title, std::string des, int publishYear, int rating, std::string var1, int var2) { int pos = this->find(title); Book *aBookPtr = dynamic_cast<Book*>(this->medias[pos]); Movie *aMoviePtr = dynamic_cast<Movie*>(this->medias[pos]); TvSeries *aTvSeriesPtr = dynamic_cast<TvSeries*>(this->medias[pos]); if(aBookPtr != nullptr) { aBookPtr->setTitle(title); aBookPtr->setDes(des); aBookPtr->setPublishYear(publishYear); aBookPtr->setRating(rating); aBookPtr->setAuthor(var1); aBookPtr->setNrOfPages(var2); } else if(aMoviePtr != nullptr) { aMoviePtr->setTitle(title); aMoviePtr->setDes(des); aMoviePtr->setPublishYear(publishYear); aMoviePtr->setRating(rating); aMoviePtr->setMainActor(var1); aMoviePtr->setMovieLength(var2); } else if(aMoviePtr != nullptr) { aTvSeriesPtr->setTitle(title); aTvSeriesPtr->setDes(des); aTvSeriesPtr->setPublishYear(publishYear); aTvSeriesPtr->setRating(rating); aTvSeriesPtr->setMainActor(var1); aTvSeriesPtr->setNrOfEpisodes(var2); } } 表示json对象的开始数组,而[表示新json对象的开始。&#34;

现在,如果你看看你的JSON,你可以看到这些

{

应该让你知道错误。

这两个都是对象的数组,但在你的第二个&#34;类&#34;有一个&#34;键&#34;在实际数组之前。所以要解决这个问题,你应该改变你的模型&#34;从此"myClass1" : [ ... ], "myClass2" : { "1" : [ ... 到这样的内容:public myClass2[,] object2 { get; set; }public KeyValuePair<string, myClass[]>[] meClass2 {get;set;}

*相关(类似)问题:*
Unexpected character encountered while parsing value: j. Path '', line 0, position 0
How to parse a json array json.net