36
[
   {
      "receiver_tax_id":"1002",
      "total":"6949,15",
      "receiver_company_name":"Das Company",
      "receiver_email":"[email protected]",
      "status":0
   },
   {
      "receiver_tax_id":"1001",
      "total":"39222,49",
      "receiver_company_name":"SAD company",
      "receiver_email":"[email protected]",
      "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());
3
  • Show us the code that tries to deserialize your JSON. Commented Dec 5, 2015 at 9:55
  • var des = (Example)JsonConvert.DeserializeObject(responseString, typeof(Example)); Console.WriteLine(des.status[0].ToString()); Commented Dec 5, 2015 at 9:57
  • And how is Example defined? Commented Dec 5, 2015 at 9:59

5 Answers 5

57

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;
Sign up to request clarification or add additional context in comments.

3 Comments

[JsonProperty("receiver_tax_id")] public string receiver_tax_id { get; set; } [JsonProperty("total")] ..... I added JsonProperty for every variable
If you use [JsonProperty("receiver_tax_id")] you can name your property as you want. [JsonProperty("status")]public int MyOwnStatus { get; set; }
I wasn't know. Thank you :)
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.

1 Comment

should be JArray.Parse (or JToken.Parse + checking deserialized.Type if we are not sure if there is array on input actually)
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);
    

1 Comment

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.
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>;

Comments

0
 var responseContent = await response.Content.ReadAsStringAsync();
 JObject joResponse = JObject.Parse(responseContent);
 JArray dataArray = (JArray)joResponse["data"];

 foreach (var item in dataArray)
 {
     Console.WriteLine("Json = " + item["id"]);
 }

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.