2017-09-21 25 views
0

をデシリアライズ私は常にエラーを取得:次のようにこのJSON文字列

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Test.Form15+results[]]' 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 'result', line 1, position 10.

私のコードがあると私はそれがJSONの開始時の問題、またはブラケットを引き起こし、二重引用符だかはわかりません文字列。任意のヒントをいただければ幸いです。

Public Class Form15 

Public Class UTicketContact 

    <JsonProperty("display_value")> 
    Public Property DisplayValue As String 

    <JsonProperty("link")> 
    Public Property Link As String 
End Class 

Public Class URequestedFor 

    <JsonProperty("display_value")> 
    Public Property DisplayValue As String 

    <JsonProperty("link")> 
    Public Property Link As String 
End Class 

Public Class AssignedTo 

    <JsonProperty("display_value")> 
    Public Property DisplayValue As String 

    <JsonProperty("link")> 
    Public Property Link As String 
End Class 

Public Class OpenedBy 

    <JsonProperty("display_value")> 
    Public Property DisplayValue As String 

    <JsonProperty("link")> 
    Public Property Link As String 
End Class 

Public Class AssignmentGroup 

    <JsonProperty("display_value")> 
    Public Property DisplayValue As String 

    <JsonProperty("link")> 
    Public Property Link As String 
End Class 

Public Class Result 

    <JsonProperty("u_ticket_contact")> 
    Public Property UTicketContact As UTicketContact 

    <JsonProperty("u_requested_for")> 
    Public Property URequestedFor As URequestedFor 

    <JsonProperty("assigned_to")> 
    Public Property AssignedTo As AssignedTo 

    <JsonProperty("opened_by")> 
    Public Property OpenedBy As OpenedBy 

    <JsonProperty("assignment_group")> 
    Public Property AssignmentGroup As AssignmentGroup 
End Class 

Public Class results 

    <JsonProperty("result")> 
    Public Property Result As Result() 
End Class 


Function FindRequestedFor(ByVal instancename As String, 
          ByVal rtask As String) As String 

    Dim requestedfor As String = "" 
    'Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData) 

    Dim accessToken As String = GenerateToken("instancenameredacted", 
               "clientIdredacted", 
               "clientSecretredacted", 
               "accountredacted", 
               "accountpasswordredacted") 

    Dim url As String = "https://" & instancename & ".service-now.com/api/ubis2/request/rtask?query=number%3D" & rtask 

    Dim request As WebRequest = WebRequest.Create(url) 
    Dim dataStream As Stream 

    request.ContentType = "application/json; charset=utf-8" 
    request.Method = "GET" 
    request.Headers.Add("Authorization", "Bearer " & accessToken) 
    dataStream = request.GetResponse.GetResponseStream 

    Dim reader As New StreamReader(dataStream) 
    Dim responseFromServer As String = reader.ReadToEnd 
'Format of the JSON string is:  ""{ 
    ""result"": [ 
    { 
     ""u_ticket_contact"": { 
     ""display_value"": ""Name1"", 
     ""link"": ""https://instance.service-now.com/api/now/table/sys_user/470104cf600ad400808370bee6ad2596"" 
     }, 
     ""u_requested_for"": { 
     ""display_value"": ""Name2"", 
     ""link"": ""https://instance.service-now.com/api/now/table/sys_user/470104cf600ad400808370bee6ad2596"" 
     }, 
     ""assigned_to"": { 
     ""display_value"": ""Name3"", 
     ""link"": ""https://instance.service-now.com/api/now/table/sys_user/98c7a3e5ac723040773cf2044a10de0c"" 
     }, 
     ""opened_by"": { 
     ""display_value"": ""Name4"", 
     ""link"": ""https://instance.service-now.com/api/now/table/sys_user/470104cf600ad400808370bee6ad2596"" 
     }, 
     ""assignment_group"": { 
     ""display_value"": ""Group Name1"", 
     ""link"": ""https://instance.service-now.com/api/now/table/sys_user_group/bad979fa19c44a40b5a0d99e2b982e75"" 
     } 
    } 
    ] 
}"" 
    Console.WriteLine(responseFromServer) 

    reader.Close() 
    dataStream.Close() 

    Dim test = JsonConvert.DeserializeObject(Of List(Of results()))(responseFromServer) 
End Function 
end class 
+0

のVisual Studioを使用**編集メニュー** - > **貼り付け** - > **貼り付けJSONクラスとして**クラスを作る。彼らは調整が必要な場合があります – Plutonix

+1

@Plutonixコードサンプルの下にスクロール – djv

答えて

0

私は以下のように初期化してList(Of Result)タイプを使用します。

Public Class results 
    <JsonProperty("result")> 
    Public Property Result As New List(Of Result) 
End Class 
+0

本当に助けていただきありがとうございます。 – Kaallis