1

Hola comunidad buenas noches podrían ayudarme por favor estoy tratando de consumir la siguiente api:

https://dummy.restapiexample.com/api/v1/employees

el json devuelto es el siguiente

{
    "status": "success",
    "data": [
        {
            "id": 1,
            "employee_name": "Tiger Nixon",
            "employee_salary": 320800,
            "employee_age": 61,
            "profile_image": ""
        },
        {
            "id": 2,
            "employee_name": "Garrett Winters",
            "employee_salary": 170750,
            "employee_age": 63,
            "profile_image": ""
        },
        {
            "id": 3,
            "employee_name": "Ashton Cox",
            "employee_salary": 86000,
            "employee_age": 66,
            "profile_image": ""
        },
        {
            "id": 4,
            "employee_name": "Cedric Kelly",
            "employee_salary": 433060,
            "employee_age": 22,
            "profile_image": ""
        },
        {
            "id": 5,
            "employee_name": "Airi Satou",
            "employee_salary": 162700,
            "employee_age": 33,
            "profile_image": ""
        },
        {
            "id": 6,
            "employee_name": "Brielle Williamson",
            "employee_salary": 372000,
            "employee_age": 61,
            "profile_image": ""
        },
        {
            "id": 7,
            "employee_name": "Herrod Chandler",
            "employee_salary": 137500,
            "employee_age": 59,
            "profile_image": ""
        },
        {
            "id": 8,
            "employee_name": "Rhona Davidson",
            "employee_salary": 327900,
            "employee_age": 55,
            "profile_image": ""
        },
        {
            "id": 9,
            "employee_name": "Colleen Hurst",
            "employee_salary": 205500,
            "employee_age": 39,
            "profile_image": ""
        },
        {
            "id": 10,
            "employee_name": "Sonya Frost",
            "employee_salary": 103600,
            "employee_age": 23,
            "profile_image": ""
        },
        {
            "id": 11,
            "employee_name": "Jena Gaines",
            "employee_salary": 90560,
            "employee_age": 30,
            "profile_image": ""
        },
        {
            "id": 12,
            "employee_name": "Quinn Flynn",
            "employee_salary": 342000,
            "employee_age": 22,
            "profile_image": ""
        },
        {
            "id": 13,
            "employee_name": "Charde Marshall",
            "employee_salary": 470600,
            "employee_age": 36,
            "profile_image": ""
        },
        {
            "id": 14,
            "employee_name": "Haley Kennedy",
            "employee_salary": 313500,
            "employee_age": 43,
            "profile_image": ""
        },
        {
            "id": 15,
            "employee_name": "Tatyana Fitzpatrick",
            "employee_salary": 385750,
            "employee_age": 19,
            "profile_image": ""
        },
        {
            "id": 16,
            "employee_name": "Michael Silva",
            "employee_salary": 198500,
            "employee_age": 66,
            "profile_image": ""
        },
        {
            "id": 17,
            "employee_name": "Paul Byrd",
            "employee_salary": 725000,
            "employee_age": 64,
            "profile_image": ""
        },
        {
            "id": 18,
            "employee_name": "Gloria Little",
            "employee_salary": 237500,
            "employee_age": 59,
            "profile_image": ""
        },
        {
            "id": 19,
            "employee_name": "Bradley Greer",
            "employee_salary": 132000,
            "employee_age": 41,
            "profile_image": ""
        },
        {
            "id": 20,
            "employee_name": "Dai Rios",
            "employee_salary": 217500,
            "employee_age": 35,
            "profile_image": ""
        },
        {
            "id": 21,
            "employee_name": "Jenette Caldwell",
            "employee_salary": 345000,
            "employee_age": 30,
            "profile_image": ""
        },
        {
            "id": 22,
            "employee_name": "Yuri Berry",
            "employee_salary": 675000,
            "employee_age": 40,
            "profile_image": ""
        },
        {
            "id": 23,
            "employee_name": "Caesar Vance",
            "employee_salary": 106450,
            "employee_age": 21,
            "profile_image": ""
        },
        {
            "id": 24,
            "employee_name": "Doris Wilder",
            "employee_salary": 85600,
            "employee_age": 23,
            "profile_image": ""
        }
    ],
    "message": "Successfully! All records has been fetched."
}

para deseralizar uso la siguiente funcion

Public Async Function GetEmployee() As Task(Of Object)
        Try
            Dim UrlApi As String = "https://dummy.restapiexample.com/api/v1/employees"
            Dim WebCliente As New WebClient()
            Dim JsonDevuleto As String = Await WebCliente.DownloadStringTaskAsync(New Uri(UrlApi))
            Dim ListaDevuelta = JsonConvert.DeserializeObject(Of List(Of EmployeeModel))(JsonDevuleto)
            Return ListaDevuelta
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try

pero obtengo el siguiente error:

Cannot deserialize the current JSON object (e.g. {""name"":""value""}) into type 'System.Collections.Generic.List`1[GestorGrados.EmployeeModel]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly." & vbCrLf & "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." & vbCrLf & "Path 'status', line 1, position 10.

¿podrían por favor ayudarme a consumir adecuadamente el API? gracias

1
  • Muestra la clase EmployeeModel
    – Yussef
    Commented el 30 mar. 2023 a las 3:40

1 respuesta 1

0

EL string que te esta devolviendo no es una simple Lista de objetos, asi que no puede deserializarlo como lo estas haciendo,

Debrias tener las clases definidas de esta forma:

Añadir una cabecera contenedora como tal y como te devuelve la API:

Public Class Cabecera
    Public Property status As String
    Public Property data As List(Of EmployeeModel)
    Public Property message As String
End Class

El contenido imagino que lo tienes correctamente y seria este:

Public Class EmployeeModel
    Public Property id As Integer
    Public Property employee_name As String
    Public Property employee_salary As Integer
    Public Property employee_age As Integer
    Public Property profile_image As String
End Class

Y luego puedes deserializar simplemente usando la clase Cabecera

Dim ObjetoDevuelto = JsonConvert.DeserializeObject(Of Cabecera)(JsonDevuleto)

Notar que no como he comentado al principio la api no devuelve una lista, y tu metodo en la firma devuelve un objeto por lo que seria posible devolverlo sin ma, si quieres que el metodo devuelva una lista deberias hacerlo de este modo:

Dim ObjetoDevuelto = JsonConvert.DeserializeObject(Of Cabecera)(JsonDevuleto)
Dim ListaDevuelta = ObjetoDevuelto.data
1

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.