How to Compare Jarray value with the Datatype using C#

Viewed 136

Here, Below is my JSON.

//MyJSON
{
    "Apply": "Access",
    "JSON": [{
        "id": 1,
        "File": "File 1",
        "Value": 0
    },
    {
        "id": 2,
        "File": "File 2",
        "Value": 0
    },
    {
        "id": 3,
        "File": "File 3",
        "Value": 1
    },
    {
        "id": 4,
        "File": "File 4",
        "Value": 1
    }]
}
//JSON 1
{
    "Apply": "Access",
    "JSON": [{
        "id": 1,
        "File": "File 1",
        "Value": 0
    },
    {
        "id": 2,
        "File": "File 2",
        "Value": 0
    }]
}
//JSON 2
{
    "Apply": "Access",
    "JSON": [{  
        "id": 3,
        "File": "File 3",
        "Value": 1
    },
    {
        "id": 4,
        "File": "File 4",
        "Value": 1
    }]
}

For making the above response I'm trying to using the below code.

var Response = JObject.Parse(MyJSON);
JArray JResponse = JArray.Parse(Response["JSON"].ToString());
foreach (var item in JResponse)
{
    //Here I tried to compare the JArray With the String
    try
    {
        Console.WriteLine(item["Value"]);
        //if (item["Value"].Equals(0))
        if (item["Value"].Equals("0"))
        {
            Console.WriteLine(item["File"]);
            Console.WriteLine(item["Value"]);
            //var JSON1 = JsonConvert.SerializeObject(json1, Newtonsoft.Json.Formatting.Indented);
        }
        else
        { 
            Console.WriteLine(item["File"]);
            Console.WriteLine(item["Value"]);
            //var JSON2 = JsonConvert.SerializeObject(json2, Newtonsoft.Json.Formatting.Indented);
        }
        
    }
    catch(Exception e)
    {
        Console.WriteLine("Exception is : " + e);
    }
        /*
        Console.WriteLine(item["id"]);
        Console.WriteLine(item["File"]);
        Console.WriteLine(item["Value"]);
        */
}

Here, I trying to compare sting value with JArray using the above code. But, Here It's not show anything as well not showing exception. It does not enter inside both if condition. Does anyone know how to compare JArray With the String/Integer value and make a separate(As it is) JSON like above?

2 Answers

You're code can be fixed and made simpler by removing a lot of unnecessary and complex code.

Please read all of the comments as they are critical in understanding the cause of your issues.

var response = JObject.Parse(MyJSON);
// We are iterating over the array property, unhelpfully named JSON.
// We do not need to serailize and deserialize this property again as
// JObject.Parse already took care of it properly.
foreach (var item in response["JSON"] ?? Enumerable.Empty<JToken>())
{
    // Important!
    // We are not comparing the array with anything.
    // item is an array element, not the array.
    // Also, the JSON contains this as a number, not a string, so we mustn't compare to "0"
    if (item["Value"] is JValue { Value: 0L }) // 0L, a long because Json.Net uses longs
    {
        Console.WriteLine("Value was 0");
        Console.WriteLine(item.ToString());
    }
    else
    {
        Console.WriteLine($"Item.Value has JToken Type of {item["Value"]?.GetType().Name} and value {item["Value"]}");
        Console.WriteLine(item.ToString());
    }
}


string MyJSON => @"
{
    ""Apply"": ""Access"",
    ""JSON"": [{
        ""id"": 1,
        ""File"": ""File 1"",
        ""Value"": 0
    },
    {
        ""id"": 2,
        ""File"": ""File 2"",
        ""Value"": 0
    },
    {
        ""id"": 3,
        ""File"": ""File 3"",
        ""Value"": 1
    },
    {
        ""id"": 4,
        ""File"": ""File 4"",
        ""Value"": 1
    }]
}
";

Here's the full code running in a .NET Fiddle

Based on your MyJson you should change your code to:

var Response = JObject.Parse(MyJSON);
JArray JResponse = JArray.Parse(Response["JSON"].ToString());
foreach (var item in JResponse)

There is no direct File property in the json, whereas there is a JSON propery that contains an array that can be parsed by JArray.parse function.

Since the JArray.parse returned an empty collection, your code never entered the foreach statement.

Related