33
[
   {
      "receiver_tax_id":"1002",
      "total":"6949,15",
      "receiver_company_name":"Das Company",
      "receiver_email":"info@another.com",
      "status":0
   },
   {
      "receiver_tax_id":"1001",
      "total":"39222,49",
      "receiver_company_name":"SAD company",
      "receiver_email":"info@mail.com",
      "status":1
   }
]

Hi, this is my Json data, but I can't deserialize it. I want to check only "status" value. (first object "status" 0, second object "status" 1).

Example definition:

public class Example 
{
    [JsonProperty("receiver_tax_id")] 
    public string receiver_tax_id { get; set; }
    [JsonProperty("total")] 
    public string total { get; set; }
    [JsonProperty("receiver_company_name")] 
    public string receiver_company_name { get; set; }
    [JsonProperty("receiver_email")] 
    public string receiver_email { get; set; }
    [JsonProperty("status")] 
    public int status { get; set; } 
}

Deserialization code:

var des = (Example)JsonConvert.DeserializeObject(responseString, typeof(Example)); 
Console.WriteLine(des.status[0].ToString());
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
Steven
  • 439
  • 1
  • 4
  • 10

4 Answers4

51

Try this code:

public class Receiver 
{
   public string receiver_tax_id { get; set;}
   public string total { get; set;}
   public string receiver_company_name { get; set;}
   public int status { get; set;}
}

And deserialize looks like follows:

var result = JsonConvert.DeserializeObject<List<Receiver>>(responseString);
var status = result[0].status;
Maxim Goncharuk
  • 1,285
  • 12
  • 20
  • [JsonProperty("receiver_tax_id")] public string receiver_tax_id { get; set; } [JsonProperty("total")] ..... I added JsonProperty for every variable – Steven Dec 05 '15 at 10:31
  • 2
    If you use `[JsonProperty("receiver_tax_id")]` you can name your property as you want. `[JsonProperty("status")]public int MyOwnStatus { get; set; }` – Maxim Goncharuk Dec 05 '15 at 10:33
  • I wasn't know. Thank you :) – Steven Dec 05 '15 at 10:35
5

If you only care about checking status you can use the dynamic type of .NET (https://msdn.microsoft.com/en-us/library/dd264741.aspx)

dynamic deserialized = JObject.Parse(responseString); 
int status1 = deserialized[0].status; 
int status2 = deserialized[1].status; 
//
// do whatever

This way you don't even need the Example class.

Dan Dinu
  • 32,492
  • 24
  • 78
  • 114
4

From your code and JSON sampels it seems the problem is you're actually deserializing a List<Example> rather than a single Example.

I would do two things:

  1. Make your class follow .NET naming conventions, as you already prefixed them with the proper JsonProperty attributes:

    public class Example 
    {
        [JsonProperty("receiver_tax_id")] 
        public string ReceiverTaxId { get; set; }
    
        [JsonProperty("total")] 
        public string Total { get; set; }
    
        [JsonProperty("receiver_company_name")] 
        public string ReceiverCompanyName { get; set; }
    
        [JsonProperty("receiver_email")] 
        public string ReceiverEmail { get; set; }
    
        [JsonProperty("status")] 
        public int Status{ get; set; } 
    }
    
  2. Deserialize a List<Example> using the generic JsonConvert.DeserializeObject<T> overload instead of the non-generic version you're currently using:

    var des = JsonConvert.DeserializeObject<List<Example>>(responseString); 
    Console.WriteLine(des[0].Status);
    
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
  • If you are having this problem, pay attention to this answer. I had an array of strings as field in the object I was having this error with. That prevented me from realizing my real problem was I was getting a list of one item at the top level and the array of strings inside that object was fine. – JohnOpincar Mar 17 '20 at 14:18
0

You're trying to deserialize an array into an Example object. Try doing it to a List instead:

var des = JsonConvert.DeserializeObject(responseString, typeof(List<Example>)) as List<Example>;
Vi100
  • 4,080
  • 1
  • 27
  • 42