Newtonsoft.Json.Linq.JArray to string array C#

C#ArraysJsonStringjson.net

C# Problem Overview


I have a JSON Array like

model.Users =  ["Joe","Barny","Power","Tester"]

the model is dynamic

I want to convert model.Users to string[] Users

string[] Users = model.Users 

How can I do that?

C# Solutions


Solution 1 - C#

If model.Users is of type Newtonsoft.Json.Linq.JArray try to call:

string[] Users = model.Users.ToObject<string[]>()

Solution 2 - C#

string[] Users = new string[20];

int i = 0;

foreach ( string item in model.Users )
{
   Users[i] = item;
   i++;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFighter PlaneView Question on Stackoverflow
Solution 1 - C#AlbertoView Answer on Stackoverflow
Solution 2 - C#kubiakView Answer on Stackoverflow