1

I have a js stringify array "["yellow", "black"]" and I'm trying to convert it to C# list

Iv' tried to do the following code

stringList.Trim('[',']').ToList();

But the result is a list if every char

[ "y","e","l","l","o","w"...]

Any ideas of how can I do that?

THX

Matan Shushan
  • 1,204
  • 1
  • 10
  • 25
  • It would be awesome if you could provide a [mcve] of your progress so far. – mjwills Jun 24 '19 at 12:01
  • Does https://app.quicktype.io?share=lbue2h2QQCHi2C3TTMS8 help? – mjwills Jun 24 '19 at 12:02
  • 1
    Possible duplicate of [How to convert Json array to list of objects in c#](https://stackoverflow.com/questions/19581820/how-to-convert-json-array-to-list-of-objects-in-c-sharp) – sr28 Jun 24 '19 at 12:03
  • 1
    You'll need to parse the JSON being sent to the server. ASP.Net will do this automatically if you specify the right model in the arguments to the controller action. – phuzi Jun 24 '19 at 12:18

2 Answers2

1

We use Newtonsoft for all JSON parsing:

string[] arr = Newtonsoft.Json.JsonConvert.DeserializeObject<string[]>("[\"yellow\", \"black\"]");
Andy Nugent
  • 859
  • 7
  • 21
1

You can use the new System.Text.Json APIs

JsonSerializer.Parse<List<string>>("[\"yellow\", \"black\"]");

For further information you can read Try the new System.Text.Json APIs.

Mustafa Gursel
  • 782
  • 1
  • 7
  • 16