cookies cookies - 19 days ago 5
C# Question

How do I convert JSON to Array

If I have the JSON string:

{"synonyms":["house","abode","appartment","condo","dwelling"]}


How do I store this in an array so that in C# it will be this?

string[] synonyms = {"house","abode","appartment","condo","dwelling"};


I am trying to use the Newtonsoft.Json library.

Answer

Create a RootObject class.

public class RootObject
{
    public List<string> synonyms { get; set; }
}

Create a RootObject by using this:

RootObject obj = JsonConvert.DeserializeObject<RootObject>(json);

// Acessing synonyms property of obj
List<string> syn = obj.synonyms; // call to Array if you want
Comments