0

Here is my JSON result from Postman

{
    "result": [
        {
            "brands": [],
            "services": [],
            "addresses": [],
            "accessPoints": [],
            "Kodu": "100",
            "name": "üeticissdss",
            "taxNumber": "asd",
            "taxOffice": "sada",
            "id": "bfb064f4-0732-43ab-9d94-87f6934258fe",
            "isActive": true
        },
        {
            "brands": [],
            "services": [],
            "addresses": [],
            "accessPoints": [],
            "Kodu": "100",
            "name": "üeticissdss",
            "taxNumber": "asd",
            "taxOffice": "sada",
            "id": "bfb064f4-0732-43ab-9d94-87f69d4258fe",
            "isActive": true
        }
    ],
    "id": 13,
    "exception": null,
    "status": 5,
    "isCanceled": false,
    "isCompleted": true,
    "isCompletedSuccessfully": true,
    "creationOptions": 0,
    "asyncState": null,
    "isFaulted": false
}

and I am trying to deserialize the json like below :

    [BindProperty]
    public List<Manufacturer> manufacturer { get; set; }
HttpClient httpClient = new HttpClient();
            httpClient.BaseAddress = new Uri(apiParameters.HttpClientBaseAddress);
            httpClient.DefaultRequestHeaders.Accept.Clear();
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(apiParameters.DefaultRequestHeadersJson));
            customIndexModel.ApiResponseMessage = await httpClient.GetAsync(apiParameters.HttpClientBaseAddress);
            if (customIndexModel.ApiResponseMessage.IsSuccessStatusCode)
            {
                var jsonAsString = customIndexModel.ApiResponseMessage.Content.ReadAsStringAsync().Result;
                manufacturer = JsonConvert.DeserializeObject<List<Manufacturer>>(jsonAsString);                    
            }

the error message :

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Q.Entity.Manufacturer.Manufacturer]' 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.

It looks like I'm miss something but I can not figure out. Could you help me please ?

Nkosi
  • 235,767
  • 35
  • 427
  • 472
S. Aziz Kazdal
  • 1,126
  • 3
  • 16
  • 40
  • 4
    Your json is a single object, not a collection (although it contains a property named `result` which is a collection. What is your `Manufacturer` model? –  Aug 21 '17 at 06:20
  • As the error message says, deserialize to a single object not a list, which you can generate by using http://json2csharp.com/ or [Paste JSON as Classes](https://stackoverflow.com/q/18526659). Thus, a duplicate of [Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type 'System.Collections.Generic.List`1](https://stackoverflow.com/q/21358493). – dbc Aug 21 '17 at 06:20
  • And see also [Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type 'System.Collections.Generic.List](https://stackoverflow.com/a/32764430). – dbc Aug 21 '17 at 06:23
  • My mistake. I was trying to deserialize single object to list. I used http://json2csharp.com/ to generate c# class for JSON. and deserialize it to RootObejct. Then I am able to get list of the result that return from JSON. – S. Aziz Kazdal Aug 21 '17 at 07:00

1 Answers1

0

this is de c# class that generated from http://json2csharp.com/

public class Result
    {
        public List<object> brands { get; set; }
        public List<object> services { get; set; }
        public List<object> addresses { get; set; }
        public List<object> accessPoints { get; set; }
        public string Kodu { get; set; }
        public string name { get; set; }
        public string taxNumber { get; set; }
        public string taxOffice { get; set; }
        public string id { get; set; }
        public bool isActive { get; set; }
    }

    public class RootObject
    {
        public List<Result> result { get; set; }
        public int id { get; set; }
        public object exception { get; set; }
        public int status { get; set; }
        public bool isCanceled { get; set; }
        public bool isCompleted { get; set; }
        public bool isCompletedSuccessfully { get; set; }
        public int creationOptions { get; set; }
        public object asyncState { get; set; }
        public bool isFaulted { get; set; }
    }

and I deserialize JSON to dataList

[BindProperty]
public RootObject dataList { get; set; }

var jsonAsString = customIndexModel.ApiResponseMessage.Content.ReadAsStringAsync().Result;
dataList = JsonConvert.DeserializeObject<RootObject>(jsonAsString);

After JSOn deserialization I am able to use result in Razor page like below

@foreach (var item in Model.dataList.result)
{
++rowNumber;
<tr>
<td>@item.code</td>
<td>@item.name</td>
<td>@item.isActive</td>
</

Thank you.

S. Aziz Kazdal
  • 1,126
  • 3
  • 16
  • 40