Get Length of array JSON.Net

77,696

Solution 1

You can cast the object to a JArray and then use the Count property, like so:

JArray items = (JArray)test["JSONObject"];
int length = items.Count;

You can then loop the items as follows:

for (int i = 0; i < items.Count; i++)
{
    var item = (JObject)items[i];
    //do something with item
}

According to Onno (OP), you can also use the following:

int length = test["JSONObject"].Count();

However, I have not personally confirmed that this will work

Solution 2

The easiest and cleanest way I found:

int length = test["JSONObject"].Count;

Solution 3

You can use below line to get the length of JSON Array in .Net (JArray) .

 int length = ((JArray)test["jsonObject"]).Count;

Solution 4

Just try this:

var test= ((Newtonsoft.Json.Linq.JArray)json).Count;

Solution 5

This worked for me supposing the json data is in a json file. In this case, .Length works but no intellisence is available:

    public ActionResult Index()
    {
        string jsonFilePath = "C:\\folder\\jsonLength.json";
        var configFile = System.IO.File.ReadAllText(jsonFilePath);

        JavaScriptSerializer jss = new JavaScriptSerializer();
        var d = jss.Deserialize<dynamic>(configFile);

        var jsonObject = d["JSONObject"];
        int jsonObjectLength = jsonObject.Length;
        return View(jsonObjectLength);
    }
Share:
77,696

Related videos on Youtube

Onno
Author by

Onno

Updated on July 09, 2022

Comments

  • Onno
    Onno almost 2 years

    How can I get the length of a JSON Array I get using json.net in C#? After sending a SOAP call I get a JSON string as answer, I use json.net to parse it.

    Example of the json I got:

    {"JSONObject": [
        {"Id":"ThisIsMyId","Value":"ThisIsMyValue"},
        {"Id":"ThisIsMyId2","Value":"ThisIsMyValue2"}
    ]}
    

    And I parse it and write it in console:

    var test = JObject.Parse (json);
    Console.WriteLine ("Id: {0} Value: {1}", (string)test["JSONObject"][0]["Id"], (string)test["JSONObject"][0]["Value"]);
    

    This works like a spell, only I don't know the length of the JSONObject, but I need to do it in a for loop. I only have no idea how I can get the length of test["JSONObject"]

    But something like test["JSONObject"].Length would be too easy I guess :(..

  • svick
    svick almost 11 years
    Don't you need to cast to JArray for that?
  • Onno
    Onno almost 11 years
    I tried that, as I seen that somewhere else. But somewhy I did not try Count() instead, what seems to work. I'm very sorry for my stupidity :O
  • musefan
    musefan almost 11 years
    @Onno: Are you saying that Count() works? If so I can update the answer
  • Onno
    Onno almost 11 years
    Yes, I just tried it and it works. Count gives me an error in the compiler
  • Andrew Jay
    Andrew Jay about 7 years
    Use ".Count", not ".Count()"
  • rr789
    rr789 almost 6 years
    If the json string includes both objects (with curly brackets {}) and arrays (with square brackets [])...I found using JObject worked with both types. JArray only worked with arrays.