2016-05-26 4 views
0

解決方法がわからないという問題があります。JSON JsonConvert.DeserializeObjectエラー

私はエラーを解決しようとしている:私は、スタックオーバーフロー上で同様のページを見つけたと回答で提案されたものを試してみました

Additional information: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'name.jsonPrjData' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly

を私は問題が何であるかを把握することはできませんよ。

Imports Newtonsoft.Json 
Imports Newtonsoft.Json.Linq 

    Public Class jsonPrjData 
     Public Property sapcode() As String 
     Public Property prjCode() As String 
     Public Property prjDescript() As String 
    End Class 

    Public Class Form1 

     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 

     End Sub 

     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
      Dim webClient As New System.Net.WebClient 
      Dim result As String = webClient.DownloadString("http://localhost/json/") 
      Dim obj = JsonConvert.DeserializeObject(Of jsonPrjData)(result) 
     End Sub 
    End Class 

とJSONのようになります。

[{"sapcode":"xxxx","prjCode":"xxxx","prjDescript":"xxxx"},{"sapcode":"xxxx","prjCode":"xxxx","prjDescript":"xxxx"}]

+1

私は、あなたがjsonオブジェクトの配列を単一のjsonPrjData .NETオブジェクトにデシリアライズしようとしていると思います。あなたはセットまたはリストにdesirializeする必要があります。 – yardpenalty

+0

どうすればいいですか? – Starlays

答えて

2

エラーメッセージの重要な部分がある:Cannot deserialize the current JSON array (e.g. [1,2,3])。角括弧は、データがアレイ(例えば、[1、2、3])と、データの2つの繰り返しセットが明らかに存在するという事実であることを示す

To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.

:何をすべきか明確に考えを与えるメッセージスクロールそこには同じ構造のものがあります。だから、:エラーメッセージの後半部分が示すように

' Note the added() 
Dim prjData = JsonConvert.DeserializeObject(Of jsonPrjData())(result) 

は、あなたもList(of T)にデシリアライズすることができます

もちろん
Dim prjList = JsonConvert.DeserializeObject(Of List(Of jsonPrjData))(result) 

対象オブジェクトをローカルに宣言されているので、彼らは唯一のものに存在していますクリックイベント。