3

ERROR

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[RechargePortal.Models.ProviderOperator]' 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 'providers', line 1, position 13.'

List<ProviderOperator> GetProvideOperator(string service)
{
    string json = new System.Net.WebClient().DownloadString("URL");
    List<ProviderOperator> ob = new List<ProviderOperator>();
    ob = JsonConvert.DeserializeObject<List<ProviderOperator>>(json);
    ob = ob.Where(x => x.Service.Equals(service)).ToList();
    return ob;
}

JSON RESULT

{
   "providers":[
      {
         "provider_id":0,
         "provider_name":"PAY2ALL",
         "provider_code":"PAY2ALL",
         "service_id":10,
         "service":"Pay2All Cash",
         "provider_image":"",
         "status":"Success"
      },
      {
         "provider_id":1,
         "provider_name":"AIRTEL",
         "provider_code":"A",
         "service_id":1,
         "service":"Recharge",
         "provider_image":"provider_icons\/airtel.png",
         "status":"Success"
      },
      {
         "provider_id":2,
         "provider_name":"VODAFONE",
         "provider_code":"V",
         "service_id":1,
         "service":"Recharge",
         "provider_image":"provider_icons\/vodafone.png",
         "status":"Success"
      }
   ]
}

Model

public class ProviderOperator 
{ 
    public string Provider_id { get; set; } 
    public string Provider_name { get; set; } 
    public string Provider_code { get; set; } 
    public string Service { get; set; } 
    public string Provider_image { get; set; } 
    public string Status { get; set; } 
}
3
  • 4
    your json isn't a list, it's an object with a list in it Commented Mar 5, 2018 at 6:15
  • 1
    You may use some online tools to ensure that you get your JSON into C# correctly: json2csharp.com Commented Mar 5, 2018 at 6:26
  • Very nice website :) - @AbdullahDibas Commented Feb 3, 2023 at 10:26

2 Answers 2

4

You should try following class:

public class ProviderOperator
{
    public List<Provider> providers { get; set; }
}

public class Provider
{
    public int provider_id { get; set; }
    public string provider_name { get; set; }
    public string provider_code { get; set; }
    public int service_id { get; set; }
    public string service { get; set; }
    public string provider_image { get; set; }
    public string status { get; set; }
}

var ob = JsonConvert.DeserializeObject<ProviderOperator>(json);

Output:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

Your Json Result is in different format and the class structure you are using to parse json is in different format. You need to create two separate classes as follows

Class Structure

 public class ProviderOperator
  {
        public List<ProviderInfo> providers { get; set; }
  }
 public class ProviderInfo
  {
        public int provider_id { get; set; }
        public string provider_name { get; set; }
        public string provider_code { get; set; }
        public int service_id { get; set; }
        public string service { get; set; }
        public string provider_image { get; set; }
        public string status { get; set; }
   }

Parsing Response

var result = JsonConvert.DeserializeObject<ProvderOperator>(json);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.