I'm working with a legacy system that is using a Generic Method to Deserialize Json Responses to Objects using Newtonsoft.Json as Follows:
responseData = JsonConvert.DeserializeObject<TResponse>(responseData);
I'm trying to deserialize this response:
[
"LA1_1200099253",
"LA1_1200030493",
"LA1_1200005581",
"LA1_1199533163",
"LA1_1199521680",
"LA1_1199500161",
"LA1_1199445213",
"LA1_1199385918",
"LA1_1198691674",
"LA1_1198584599",
"LA1_1198580864",
"LA1_1198199891",
"LA1_1198193839",
"LA1_1197677005",
"LA1_1197387180",
"LA1_1197178604",
"LA1_1197195621",
"LA1_1197149865",
"LA1_1197164149",
"LA1_1197050213"
]
But I'm getting this exception:
Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: [. Path '', line 1, position 1.
When trying to map the response to this class:
public class MatchesResponse : ResponseBase
{
public List<string> Games { get; set; } = new List<string>();
}
Does anyone know what I could do?. Thanks in Advance
CodePudding user response:
If responseData is a JSON string as in your example, then result can't be a responseData as well. You should rename it to something else in the first place. Simply you can user string[]:
string responsedata = [
"LA1_1200099253",
"LA1_1200030493",
"LA1_1200005581",
"LA1_1199533163",
"LA1_1199521680",
"LA1_1199500161",
"LA1_1199445213",
"LA1_1199385918",
"LA1_1198691674",
"LA1_1198584599",
"LA1_1198580864",
"LA1_1198199891",
"LA1_1198193839",
"LA1_1197677005",
"LA1_1197387180",
"LA1_1197178604",
"LA1_1197195621",
"LA1_1197149865",
"LA1_1197164149",
"LA1_1197050213"
];
var result = JsonConvert.DeserializeObject<string[]>(responseData);
foreach(string s in result)
{
Console.WriteLine(s);
}
CodePudding user response:
deserialize object allows us to parse our c# properties into a json object. if the expressions you define in the array correspond to the object, you can use this method JsonConvert.DeserializeObject<List<"Class Name">>(json);
CodePudding user response:
try this
var response = new MatchesResponse
{
Games = JsonConvert.DeserializeObject<string[]>(responseData);
}
