C# 如何使用C将Jarray值与数据类型进行比较#

C# 如何使用C将Jarray值与数据类型进行比较#,c#,json,C#,Json,下面是我的JSON //MyJSON { "Apply": "Access", "JSON": [{ "id": 1, "File": "File 1", "Value": 0 }, { "id": 2, "File&

下面是我的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
    }]
}
为了做出上面的回答,我尝试使用下面的代码

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"]);
        */
}
在这里,我试图使用上面的代码将sting值与JArray进行比较。但是,这里不显示任何内容,也不显示异常。它不会进入两个if条件的内部。
是否有人知道如何将JArray与字符串/整数值进行比较,并创建一个单独的(按原样)JSON(如上所述)?

基于您的
MyJson
您应该将代码更改为:

var Response = JObject.Parse(MyJSON);
JArray JResponse = JArray.Parse(Response["JSON"].ToString());
foreach (var item in JResponse)
json中没有直接的
文件
属性,但是有一个
json
属性,它包含一个可以由
JArray.parse
函数解析的数组


由于
JArray.parse
返回了一个空集合,因此您的代码从未进入
foreach
语句。

您的代码可以通过删除大量不必要的复杂代码来修复并简化

请阅读所有评论,因为它们对于理解问题的原因至关重要

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
    }]
}
";
var response=JObject.Parse(MyJSON);
//我们正在对数组属性进行迭代,该属性的名称为JSON,没有任何帮助。
//我们不需要将此属性再次序列化和反序列化为
//Parse已经正确地处理了它。
foreach(响应中的var项[“JSON”]??Enumerable.Empty()
{
//重要!
//我们没有将数组与任何东西进行比较。
//项是数组元素,而不是数组。
//此外,JSON将其作为数字而不是字符串包含,因此我们不能将其与“0”进行比较
如果(item[“Value”]是JValue{Value:0L})//0L,则为long,因为Json.Net使用long
{
Console.WriteLine(“值为0”);
Console.WriteLine(item.ToString());
}
其他的
{
WriteLine($”Item.Value具有JToken类型{Item[“Value”]?.GetType().Name}和值{Item[“Value”]});
Console.WriteLine(item.ToString());
}
}
字符串MyJSON=>@”
{
“应用”:“访问”,
“JSON”:[{
“id”:1,
“”文件“”:“”文件1“”,
“”值“”:0
},
{
“id”:2,
“”文件“”:“”文件2“”,
“”值“”:0
},
{
“id”:3,
“”文件“”:“”文件3“”,
“值”:1
},
{
“id”:4,
“文件”:“文件4”,
“值”:1
}]
}
";

下面是运行在a

中的完整代码,为什么要将数组转换回字符串,用
JArray.parse(Response[“JSON”].ToString())对其进行解析?直接用
Response.get(“JSON”)
之类的东西获取值不是更好吗?我知道这一点。由于错误,我编写了文件而不是JSON。我编辑了我的问题。