97

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?

1
  • 3
    Have you attempted anything? What is the definition of model.Users because it seems like it is a string[] already. The "JSON.Array" you supplied doesn't look like JSON, it looks more like an array declaration. Commented Nov 12, 2014 at 9:26

2 Answers 2

222

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

string[] Users = model.Users.ToObject<string[]>()
Sign up to request clarification or add additional context in comments.

Comments

-5
string[] Users = new string[20];

int i = 0;

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

3 Comments

if you are adding a new answer to a very old question which already has so many answers, try and add a sentence explaining why and how this is better or different.
The provided answer was flagged for review as a Low Quality Post. Here are some guidelines for How do I write a good answer?. This provided answer could benefit from an explanation. Code only answers are not considered "good" answers. From Review.
This doesn't work if the amount of users returned is greater than 20. And 20 is just an arbitrary "magic number", so that doesn't even necessarily fit the OPs business logic requirements.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.