0

I am Deserializing Json with below code.

public class MyArray
        {
            public int RequestID { get; set; }
            public int Status { get; set; }
            public string ResponseMessage { get; set; }
        }
    
        public class Root
        {
            public List<MyArray> MyArray { get; set; }
        }
   var rawJson = "[{\"RequestID\":12345,\"Status\":100,\"ResponseMessage\": \"API Call Successful\"}]";

 var myDeserializedClass1 = JsonConvert.DeserializeObject<Root>(rawJson); 

I am getting below error. It is not populating the the My Array. Any thoughts on what's going wrong here.

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'xyz.Root' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
1
  • Raw json is simply one element... not an array. Deserialize to MyArray, not List of MyArray Commented Oct 29, 2020 at 14:04

3 Answers 3

1

Update Based on change in OP

As per your latest edit in the question, you have an array of MyArray. You should hence deserialize using a collection of MyArray.

For example,

 var myDeserializedClass1 = JsonConvert.DeserializeObject<MyArray[]>(rawJson); 
// OR
 var myDeserializedClass1 = JsonConvert.DeserializeObject<IEnumerable<MyArray>>(rawJson); 

Based on Original Question before the Edit

You are attempting to deserialize using wrong object type (Root), which is why it fails to do so, and returns a null value.

Your Json contains a single instance of MyArray and not an instance of Root. You need to deserialize using

var myDeserializedClass1 = JsonConvert.DeserializeObject<MyArray>(rawJson); 

That would provide you the desired result.

enter image description here

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

1 Comment

Thanks for the answer, it is working this way. Please recheck the question, i have done some edits.
1

Your JSON simply doesn't contain an Array. A JSON array typically starts with a [ and ends with a ].

So either you change your JSON to this:

[
  {
    "RequestID": 12345,
    "Status": 100,
    "ResponseMessage": "API Call Successful"
  }
]

Or try and deserialize to the MyArray object immediately like so:

var deserializedObject = JsonConvert.DeserializeObject<MyArray>(json); 

1 Comment

[] This was there previously, i removed it. I have edited question now. please check.
1

If you wanted to make it deserializable to Root object, 'rawJson' should be something like this:

var rawJson = "{\"MyArray\":[{\"RequestID\":12345,\"Status\":100,\"ResponseMessage\": \"API Call Successful\"}]}";

By the way, if your Root class does not have any other data or behavior, you can simply desrialize a List of MyArray:

var rawJson = "[{\"RequestID\":12345,\"Status\":100,\"ResponseMessage\": \"API Call Successful\"}]";
 var myDeserializedObject = JsonConvert.DeserializeObject<List<MyArray>>(rawJson); 

In the code above, myDeserializedObject is of type List. You can also deserialize this to any collection like MyArray[], IList<MyArray> or enumerable IEnumerable<MyArray>.

take a look at the live code in this dot net fiddle file I created.

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.