2016-10-23 5 views
0

私は、次の形式で値を返すされているWeb ASPのAPIがあります。エラーが発生する理由現在のJSONオブジェクトを逆シリアル化できませんか?

{"idDoc":18,"idDocRipristino":0,"relCau":1,"numDoc":"2","data":"2016-10-17T00:00:00","relTavolo":3,"apertura":"4","orario":"2016-10-17T00:00:00","idAna":0,"nominativo":"5","relOpe":6,"totale":7.0,"note":"8","idAsp":0,"romana":0,"relOpe_Port":0}

をそして、私はDevExpress社のGridViewのにデータをバインドするためにWinフォームアプリケーションから以下のコードを使用していますが、私はエラーの下に取得しています:ここでは

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

は私のWinフォームのコードは、APIにアクセスし、JSONを使用してデータを読み込むことです:

 Async Sub GetDocumenti(idDoc As Integer) 
    Dim Uri As String = "http://localhost:53917/api/Documenti/GetByIdDoc" 
    Using client = New HttpClient() 
     Using response = Await client.GetAsync([String].Format("{0}/{1}", Uri, idDoc)) 
      If response.IsSuccessStatusCode Then 
       Dim DocumentisonData = Await response.Content.ReadAsStringAsync() 
       GridCtrlDocumenti.DataSource = JsonConvert.DeserializeObject(Of Documenti())(DocumentisonData).ToList() 
      Else 
       Dim result = DevExpress.XtraEditors.XtraMessageBox.Show("Sorry no data found!!", "Confirmation Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) 

      End If 
     End Using 
    End Using 
End Sub 

これは私のリポジトリである:

public DocumentiModel GetByIdDoc(int id) 
    { 
     using (var dbCtx = new USDevEntities()) 
     { 
      var documenti = dbCtx.Documentis.Where(x => x.IDDoc == id).FirstOrDefault(); 
      if (documenti != null) 
      { 
       return ConvertTo(documenti); 
      } 
      else 
      { 
       return null; 
      } 
     } 
    } 

このエラーを解決する方法を教えてください。ありがとう。

+0

JSONがちょうど単一のオブジェクトで、まだあなたは配列としてそれをシリアル化してみてください。 – Styxxy

+0

私はJSONの新機能です。私の問題を解決する方法や答えを説明する例を教えてください。 – barsan

+0

'Documenti'クラスを表示してください – Jim

答えて

3

JSONは、コードが配列に逆シリアル化しようとしている間に、単一のオブジェクトを表します。それは動作しません。

変更この行:これに

GridCtrlDocumenti.DataSource = JsonConvert.DeserializeObject(Of Documenti())(DocumentisonData).ToList() 

Dim list As List(Of Documenti) = New List(Of Documenti) 
list.Add(JsonConvert.DeserializeObject(Of Documenti)(DocumentisonData)) 
GridCtrlDocumenti.DataSource = list 
+0

ありがとうございました。それは今働いている。再度、感謝します。 – barsan

+1

うれしい私は助けることができます。 –

+0

絶対にあなたは私の時間を救った。 +1 – barsan

関連する問題