1

I am trying to deserialize a GraphQLResponse in c#.

My response contains the following:

{
  "myClassObjects": [
    {
      "var1": "var1Value",
      "var2": "var2Value"
    }, //more MyClass objects
]}

According to several sources, I should now be able to unpack and deserialize this using:

var output = response.getDataFieldAs<IEnumerable<MyClass>>("myClassObjects");

Sadly, the getDataFieldAs method does not exist (anymore).

The response.Data is a Newtonsoft.Json.Linq.JObject which contains an object "myClassObjects" which is an array of MyClass objects. When I try to unpack :

List<MyClass> output = JsonConvert.DeserializeObject<List<MyClass>>(response.Data.ToString());

I keep getting :

Cannot deserialize the current JSON object ... because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly

It's my first time working with Json and I really hope there is a handy sollution for this ? Any help would be much appreciated.

1 Answer 1

2

you're missing the rootObject when deserializing the "array" of, what i am assuming, is your MyClass objects.

public class RootObject {
  [JsonProperty("myClassObjects")]
  public List<MyClass> MyClassObjects {get;set; }
}

// then deserialize your json like this,
var obj = JsonConvert.DeserializeObject<RootObject>(response.Content); 

Without the details on how you are getting the response, i am not sure how to explain the difference in response.Content vs response.Data. If you are using RestSharp, then Data would have the correctly deserialized data for you. Content is the raw string that you would deserialize yourself.

The error you keep getting is because you are deserializing the Json as [ ] but its { property: value }. Your json has an object that has an array instead of json being an array of objects.

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

5 Comments

Thank you so very much! I can now deserialize and create a RootObject. Giving a getType on obj confirms that this step works. Using a string that contains an object that is an array containing ten MyClass objects. But inside the RootObject, the List MyClassObjects is null (??). ..... I can deserialise a MyClass from a string. I have tried flagging MyClass with a JsonProperty tag, I tried flagging the MyClass constructor, but neither accept this tag.
@RobGreen can you share your MyClass
Class is like this: ``` public class ManagedAsset{ public ManagedAsset() { } DateTime MWAMostRecentMaintenance { get; set; } string MWAName { get; set; }} ```
I tried to deserialize from string and that works: string testJsonString = "{\"MWAMostRecentMaintenance\": \"2020-08-05\",\"MWAName\": \"randomName\"}"; var unpackJson1 = JsonConvert.DeserializeObject<ManagedAsset>(testJsonString); Debug.WriteLine("Mark 8 - created a ManagedAsset by Deserialising a string. Type is: " + unpackJson1.GetType()); >>> this gives: created a ManagedAsset by Deserialising a string. Type is: test1.Model.ManagedAsset
@RobGreen make sure that the name of the properties in the class match up with the property names in the json. These are case sensitive as well.

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.