2017-05-17 18 views
1

を解析する:失敗は、私はこのようなJSON持つ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 occurred
HResult=0x80131500 Message=Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'CreativaTV.Soporte[][]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'soportes.2', line 185, position 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) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, 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 Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent) at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings) at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings) at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value) at CreativaTV.Form1.CargarContenido() in C:\AppDev\CreativaTV\CreativaTV\CreativaTV\Form1.cs:line 61 at CreativaTV.Form1.Form1_Load(Object sender, EventArgs e) in C:\AppDev\CreativaTV\CreativaTV\CreativaTV\Form1.cs:line 32 at System.Windows.Forms.Form.OnLoad(EventArgs e) at System.Windows.Forms.Form.OnCreateControl() at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at 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) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

+0

次のエラーメッセージのどの部分を理解していませんか? – SLaks

+0

私はCreativaTV.Soporte [] [] 'が表示されているコードのどこにも表示されません(コードとエラーは同じコードからのもので、名前は付けられていないはずです)。 – crashmstr

答えて

2

それがあるべきようになっています:

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

Dictionary<int, myClass2[]>も動作するようですが、私はエラーを持っています。

1

同じ質問を何度も何度も同じ答えを得ているが、私はちょうどコピーして、「同じ質問」から私の答えを貼り付けます:あなたは[は初めを示していることがわかりますJSONの仕様では、」

{は新しいjsonオブジェクトの開始を示しています。

あなたはJSONを見れば今、あなたはあなたに何が間違っているアイデアを与える必要があり、これらの

"myClass1" : [ 
... 
], 
"myClass2" : { 
    "1" : [ 
... 

を見ることができます。

これらはどちらもオブジェクトの配列ですが、2番目の「クラス」には実際の配列の前に「キー」があります。だからこれを修正するには、あなたの "モデル"を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

+1

答えをコピーするのではなく、重複してフラグを立てるべきだと思います。または、元の回答へのリンクも含める必要があります。 – Marie

+0

@マリーIMOそれは役に立たない。 Json.Netに関する質問は、実際にJSON文字列がどのように見えるかのコンテキスト内でのみ異なります。残りは同じままです。 –

関連する問題