0

I want to know how can I convert(Deserialize) a json array of json array to a list of string.

which means that inner array should be converted into string

the json is :

[
      [
         "a",
         "b",
         "c",
         null,
         1
      ],
      [
         "d",
         "e",
         null,
         2
      ]
]

the c# code using built-in c# json deserializer is :

List<string> myList = System.Text.Json.JsonSerializer.Deserialize<List<string>>(json);

This exception occurs :

enter image description here

And Newtonsoft :

List<string> myList = JsonConvert.DeserializeObject<List<string>>(json);

enter image description here

After I couldn't deserialize this json (which is google translate api response) with built-in deserializer in dotnetcore 3.1 and Newtonsoft , I decided to convert it manually to classes and strings but my code didn't work.

the result should be like that :

list :

item 1 :

[
        "a",
        "b",
        "c",
        null,
        1
]

item 2 :

[
        "d",
        "e",
        null,
        2
]
  1. Is there a way to deserialize the json I mentioned in the link into classes ? (Visual Studio Special Paste didn't work)

  2. Why I cannot convert json array of json array into List of string ?

  3. Is this problem related with this issue ?

7
  • 1
    d, in json looks like a syntax error. 1 and 2 looks like int but not string. Commented Sep 20, 2020 at 18:42
  • 1
    Deserialize it into a nested collections (an array of string arrays, or something similar), and then walk through the results, adding each string to a list you create Commented Sep 20, 2020 at 18:43
  • 1
    Also you have array of arrays here, not array of string. Commented Sep 20, 2020 at 18:47
  • 1
    How should resulting string look? Commented Sep 20, 2020 at 18:50
  • 1
    @Parsa see the List<JArray> version in my answer. You can use Formatting.Indented to get multiline strings. Commented Sep 20, 2020 at 18:57

1 Answer 1

2

You have not only string in your collection and you have nested arrays, so List<string> does not represent your JSON structure. If you want to get only string you can do something like this (this one is with Newtonsoft, after fixing the d value):

var strings = JsonConvert.DeserializeObject<List<List<object>>>(json)
    .Select(arr => arr.OfType<string>().ToList())
    .ToList();

Or using arr => arr.Select(a => a?.ToString() in Select if you want to convert all values to strings.

Or you can convert to List<JArray> with Newtonsoft and call ToString on it:

List<string> strings = JsonConvert.DeserializeObject<List<JArray>>(json)
    .Select(jarr => jarr.ToString(Newtonsoft.Json.Formatting.None)) 
    .ToList();
Console.WriteLine(string.Join(", ", strings)); // prints "["a","b","c",null,1], ["d","e",null,2]"
Sign up to request clarification or add additional context in comments.

2 Comments

your code works perfectly ! , thank you , do you know how can I make a c# class model for the json I provided in that link ? the visual studio couldn't do that. online sites couldn't too.
@Parsa I'm afraid only something similar to List<List<object>> or List<JArray> or List<JToken> or just JToken with next manual handling. TBH it looks like somebody used arrays in few places where JSON object should be.

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.