deserializing a JSON array that initial response is a value?

  • Thread starter Thread starter G-Oker
  • Start date Start date
G

G-Oker

Guest
Hi,

I am trying to deserialise a JSON response, whos first item is a value.

{"0000001":{"rowID":224512,"orderID":222222,"name":"gift"," Code":"DWRT2","clientCode":null,"validDate":"2020-02-10 14:02:51","redDate":null,"exDate":"2021-02-10 23:59:59","graceyDate":null,"status":"Valid","subtotal":50,"discount":0,"total":50,"meta":{"_message":null},"class":"monetary","domainID":3695,"payment":"Offline","currencyCode":"GBP","partially":false,"remaining":50,"selectedOptions":{"Value":{"value":"50"}}},
"00100000":{"rowID":358621,"orderID":222222,"name":"gift"," Code":"WYT6C","clientCode":null,"validDate":"2020-02-10 14:02:51","redDate":null,"exDate":"2021-02-10 23:59:59","graceDate":null,"status":"Valid","subtotal":50,"discount":0,"total":50,"meta":{"_message":null},"class":"monetary","domainID":3695,"payment":"Offline","currencyCode":"GBP","partially":false,"remaining":50,"selectedOptions":{"Value":{"value":"50"}}}}

etc (condensed for brevity).

If I use the (current) way of doing it I know, which is "using Newtonsoft.Json;" ), create a Class(es) to home the response, and then use

List<RootObject> Response = JsonConvert.DeserializeObject<List<RootObject>>(responseString);


then then a foreach to get the data

foreach( var item in Response )
{
var firstItem = item.FirstiteminClass.ToString();
...
}

I get the error...


Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[GiftProInterface.Rows_RootObject]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.


...Which I think is because I my class doesn't include the "value only" item which starts each record.

I cannot create the classes correctly ( even using http://json2csharp.com/ ) due to this first item not have a Title/ Value setup.


my question is, how to a deserialzie what i believe is an array of strings (which then has it own List under the initial Value) to and can then perform action son the results (per record ) ?


I hope I am making sense...


Thanks in advance

Continue reading...
 
Back
Top