-4

Need to de-serialize an array of array response to List<Model>

This is my response from other endpoint:

"[[{"BillRateCardId":6992,"ExternalRateCardId":"cd77d9b5-a00e-4696"}]]"

Am using the below line of code to deserialize it to the Model but receiving the error "cant de-serialize that":

List<Model> Res = JsonConvert.DeserializeObject<List<Model>>(Response);

2 Answers 2

1

You need to specify the data type that List is expected to contain, either by modeling it as an object:

using Newtonsoft.Json;
var input = "[[{\"BillRateCardId\":6992,\"ExternalRateCardId\":\"cd77d9b5-a00e-4696\"}]]";
var deserialized = JsonConvert.DeserializeObject<List<List<ResponseModel>>>(input);
class ResponseModel
{
    public int BillRateCardId { get; set; }
    public string? ExternalRateCardId { get; set; }
}

or by using dynamic, but then you lose static type checking:

using Newtonsoft.Json;

var input = "[[{\"BillRateCardId\":6992,\"ExternalRateCardId\":\"cd77d9b5-a00e-4696\"}]]";
var deserialized = JsonConvert.DeserializeObject<List<List<dynamic>>>(input);

Console.WriteLine(JsonConvert.SerializeObject(deserialized));
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. I have used var externalRateCardIdsResponse = JsonConvert.DeserializeObject<List<Model>>(externalRateCardIdsLambdaResponse); But was receiving error. when changed my code to var externalRateCardIdsResponse = JsonConvert.DeserializeObject<List<List<Model>>>(externalRateCardIdsLambdaResponse); I cant access the properties in it. if (Res.Where(x => x.ExternalRateCardId == (string)rateCard.SelectToken("ExternalRateCardId")) != null) {Sourceid = Res.Where(x => x.ExternalRateCardId == (string)rateCard.SelectToken("ExternalRateCardId")).Select(x => x.BillRateCardId).FirstOrDefault();}
@karthikeyansivakumar - of course, it is a list of lists, you must loop through the outer list, then through each inner list. Do you know how to do that? If not, see e.g. How to get elements of a List of Lists in C# using linq? or How to Foreach() list of lists? or Iterate over list of lists by the X coordinate?. If you are still unsure, you should ask another question, as the preferred format for questions is one question per post.
@karthikeyansivakumar - How to merge a list of lists with same type of items to a single list of items? can be used to convert a List<List<ResponseModel>> to a List<ResponseModel> if that is what you require.
0

since you have only one value, you don't need the custom class

var data =  JArray.Parse(Response);
    
long billRateCardId = data.SelectMany(x=>x)
.Where(x =>(string) x["ExternalRateCardId"]=="cd77d9b5-a00e-4696")
.Select(x=>(long) x["BillRateCardId"]).FirstOrDefault();

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.