19

Given an array in the JSON, I am trying to find the best way to convert it to JArray. For example - consider this below C# code:

var json = @"{
  ""cities"": [""London"", ""Paris"", ""New York""]
}";

I can read this JSON into JObject as -

var jsonObject = JObject.Parse(json);

Now I will get the "cities" field.

var jsonCities = jsonObject["cities"];

Here I get jsonCities as type JToken. I know jsonCities is an array, so I would like to get it converted to JArray. The way I do currently is like this -

var cities = JArray.FromObject(jsonCities);

I am trying to find out is there any better way to get it converted to JArray. How are other folks using it?

tyrion
  • 714
  • 2
  • 7
  • 27
  • 12
    You don't need to convert it to a `JArray` -- it already is a `JArray`. You just need to cast it: `var jsonCities = jsonObject["cities"] as JArray` – dbc Oct 18 '17 at 16:37
  • I understand the question is subjective so there may not be one right answer, may be however I like this approach. I didn't realize that I can simply cast it. I am hopeful that this would be more performant than the one I have been using. Not sure if the performance concern deserves a separate question. – tyrion Oct 21 '17 at 20:44

2 Answers2

27

The accepted answer should really be the comment by dbc.

After the proposed casting to JArray, we can validate the result by checking for null value:

var jsonCities = jsonObject["cities"] as JArray;
if (jsonCities == null) return;

...do your thing with JArray...

Edit:

As stated by dbc, a JToken that represent a JArray, is already a JArray. That is if the JToken.Type equals an JTokenType.Array. If so it can be accessed by using the as JArray notation. When the as casting notation is used, a failed cast will render a null value, as explained here. That makes it convenient for validating that you actually got a JArray you can use.

JArray.FromObject(x) takes an object, so it can be used with anything that can be represented as an object and thus certainly an JToken.

In this case we know that we can simply cast from JToken to JArray, so it gives us another possibility. I would expect it to be faster, but I leave that as an exercise for someone else to figure out.

pekaaw
  • 2,309
  • 19
  • 18
-2

Here we make use of c# classes to store the contents of our jsonString by deserialising the string.

Below is a basic example.

For Further Reading I will point you to the Newtonsoft.Json website.

https://www.newtonsoft.com/json/help/html/DeserializeObject.htm

class Program
{
    static void Main(string[] args)
    {

        var json = @"{""cities"": [""London"", ""Paris"", ""New York""]}";

        MyObject result = JsonConvert.DeserializeObject<MyObject>(json);

        foreach (var city in result.Cities)
        {
            Console.WriteLine(city);
        }

        Console.ReadKey();
    }

    public class MyObject
    {
        [JsonProperty("cities")]
        public List<string> Cities { get; set; }
    }
}
Derek
  • 8,300
  • 12
  • 56
  • 88
  • FYI, the OP already is using Json.Net. `JObject.Parse` is part of that library. See [LINQ-to-JSON](https://www.newtonsoft.com/json/help/html/LINQtoJSON.htm) in the Json.Net documentation. – Brian Rogers Oct 18 '17 at 17:15
  • Thanks for the answer. I don't have liberty to add a C# model class (MyObject above) in my case. – tyrion Oct 21 '17 at 20:46