0

I am trying to convert Json Response to C# object. My code is as below.

$ HttpResponseMessage response = client.GetAsync(TARGETURL).Result;

        HttpContent content = response.Content;

        // ... Check Status Code                                
        Console.WriteLine("Response StatusCode: " + (int)response.StatusCode);

        // ... Read the string.
        string result = content.ReadAsStringAsync().Result;

        Environment myJsonResponse = Newtonsoft.Json.JsonConvert.DeserializeObject<Environment>(result);
        Console.Write(myJsonResponse.id);

My Json Object Class is:

public class Environment
{
public string id { get; set; }
public string url { get; set; }
public string name { get; set; }
public string error { get; set; }
public int container_hosts_count { get; set; }
}

Result string is:

"[{\"id\":\"23745576\",\"url\":\"https://cloud.mycloud.com/configurations/23745576\",\"name\":\"mycloud Code Screen - Andy Pande\",\"error\":\"\",\"container_hosts_count\":0}]"

I am getting an error as:

Newtonsoft.Json.JsonSerializationException occurred
  HResult=0x80131500
  Message=Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Environment' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path '', line 1, position 1.
  • You're passing in an array, not an object (see the first character is `[` not `{`). – Dai Oct 08 '17 at 22:09
  • Your JSON consists of an array of `Environment`s with only a single item. Simply parse as `Newtonsoft.Json.JsonConverter.DeserializeObject>(result);` – nbokmans Oct 08 '17 at 22:09

1 Answers1

2

As per the error, you are trying to deserialize a JSON array into a single object. Update your deserialization to:

var myJsonResponse = Newtonsoft.Json.JsonConvert.DeserializeObject<IList<Environment>>(result);
d.moncada
  • 16,900
  • 5
  • 53
  • 82
  • I tried using an array for Environments as well but it dint work for me. public class Environments { public Environment[] env { get; set; } } public class Environment { public string id { get; set; } public string url { get; set; } public string name { get; set; } public string error { get; set; } public int container_hosts_count { get; set; } } –  Oct 08 '17 at 22:11
  • @RitujaDange don't add a new class called Enviroments. Keep it with just one single class "Environment", but when you call Deserialize, declare a list. See my answer. – d.moncada Oct 08 '17 at 22:15
  • Ignore my previous comment. It worked.. Thanks Moncada. –  Oct 08 '17 at 22:16
  • @RitujaDange glad to hear. can you please accept the the answer by clicking the check-mark. This will help others that may come across a similiar problem. – d.moncada Oct 08 '17 at 22:17