Convert String to Array using JArray.Parse in C#

Convert String to Array using JArray.Parse in C#

In this article we will see how we can parse an array json from JArray.Parse method using Newtonsoft library. You need to import Newtonsoft json library from NuGet and add following reference in your code:

using Newtonsoft.Json.Linq;

Create an array:

string cities=@"[
	'Pune',
	'Mumbai',
	'Nasik',
	'Aurangabad'
]";

Code to parse:

string cities=@"[
	'Pune',
	'Mumbai',
	'Nasik',
	'Aurangabad'
]";

//Parase string into array
JArray objArray = JArray.Parse(cities);

//Print json
Console.WriteLine(objArray.ToString());

Output:

{
  "cities": [
    "Pune",
    "Mumbai",
    "Nasik",
    "Aurangabad"
  ]
}

Leave a Reply

Your email address will not be published. Required fields are marked *