-2

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

1
  • 1
    do you mean DeserializeObject<string[]>? Commented Jan 23, 2022 at 21:15

4 Answers 4

1

try this

var response = new MatchesResponse 
{
  Games = JsonConvert.DeserializeObject<string[]>(responseData);
}
Sign up to request clarification or add additional context in comments.

Comments

0

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);
}

Comments

0

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);

1 Comment

This will not work as the class the OP has listed has a property called games which does not exist in the json.
0

I had the same problem and this answer helped be to solve it: https://stackoverflow.com/a/48404221/11212251

For your case it would look like this:

[JsonArray]
public class MatchesResponse : ICollection<string>
{
   public ICollection<string> Games { get; }

   public int Count => Games.Count();

   public bool IsReadOnly => Games.IsReadOnly;

   public void Add(string item)
   {
      Games.Add(item);
   }

   public void Clear()
   {
      Games.Clear();
   }

   public bool Contains(string item)
   {
      return Games.Contains(item);
   }

   public void CopyTo(string[] array, int arrayIndex)
   {
      Games.CopyTo(array, arrayIndex);
   }

   public bool Remove(string item)
   {
      return Games.Remove(item);
   }

   public IEnumerator<string> GetEnumerator()
   {
      return Games.GetEnumerator();
   }

   IEnumerator IEnumerable.GetEnumerator()
   {
      return Games.GetEnumerator();
   }
}
var deserializedData = JsonConvert.DeserializeObject<MatchesResponse>(jsonString);

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.