1

I am looking to send this array of JSON strings to my API. I am having few issues.....

  • should my class model be an array as well ?
  • When I deserialize, I get an error message: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Model' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

Body to send is:

 [
{
    "ObjectId" : "1270583B-208A-427F-8B1E-B20E6271D0FE",
    "ObjectTypeId" : 1
},
{
    "ObjectId" : "1270583B-208A-427F-8B1E-B20E6271D0FE",
    "ObjectTypeId" : 2
},
{
    "ObjectId" : "1270583B-208A-427F-8B1E-B20E6271D0FE",
    "ObjectTypeId" : 3
}
]

My model class is :

class Model
{

        public string ObjectId { get; set; }
        public int ObjectTypeId { get; set; }


}

I am deserializing my Httpcontent like that:

static object Content;
string HttpContent= "[{\"ObjectId\" : \"1270583B-208A-427F-8B1E- B20E6271D0FE\" , \"ObjectTypeId\" : 1 }]";
Content = JsonConvert.DeserializeObject<Model>(Httpcontent);
3
  • 1
    deserializing into Model[] should work. Commented Apr 23, 2018 at 18:10
  • That worked :) Thank you Commented Apr 23, 2018 at 18:17
  • deserializing into List<Model> would also work (the one answer you got...can mark that as answer I suppose) Commented Apr 23, 2018 at 18:20

2 Answers 2

1

You should try to deserialize as a List of Model objects.

Content = JsonConvert.DeserializeObject<List<Model>>(Httpcontent);
Sign up to request clarification or add additional context in comments.

Comments

0

The solution is to add [] to my model. Thank you @Johannes.colmsee

Content = JsonConvert.DeserializeObject<Model[]>(Httpcontent);

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.