【问题标题】:Check if JSON key value is different than value C#检查 JSON 键值是否不同于值 C#
【发布时间】:2020-03-03 13:26:26
【问题描述】:

我有一个像这样的JSON

{ 
"content": {
      "shopCart": {
        "numberOfItem": "string",
        "shipCostsAmount": "string"
      },
      "items": {
        [
        {
          "itemQuantity": "1",
          "itemAmount": "200",
          "itemShelf": "string",
        },
        "item2": {
          "itemQuantity": "1",
          "itemAmount": "250",
          "itemShelf": "string",
        },
        "item3": {
          "itemQuantity": "1",
          "itemAmount": "3000",
          "itemShelf": "string",
        }
       ],
      }
   }
}

我想检查itemShelf 是否不同于Liquor,例如在所有三个项目中。

我可以为这样的一个项目做到这一点:

public bool CheckItemShelf(Content content)
{
     var itemShelf = content.Items.Item1.itemShelf;
     bool result;
     if ((itemShelf == "Liquor"))
        result = false;
     else
        result = true;
     return result;
}

我怎样才能为三个(或更多)项目做到这一点?

编辑:

这是 Content 类:

  public class Content {
    /// <summary>
    /// Gets or Sets ShopCart
    /// </summary>
    [DataMember(Name="shopCart", EmitDefaultValue=false)]
    [JsonProperty(PropertyName = "shopCart")]
    public ShopCart ShopCart { get; set; }

    /// <summary>
    /// Gets or Sets Items
    /// </summary>
    [DataMember(Name="items", EmitDefaultValue=false)]
    [JsonProperty(PropertyName = "items")]
    public Items Items { get; set; }
}

编辑 2:

我将 Items 更改为数组,我如何检查所有 itemShelf 值都不是 Liquor 并且有真假响应?

【问题讨论】:

  • 发送一个字符串和你想检查的项目给函数而不是内容本身
  • 尝试使用库来处理 JSON 数据。类似Newtonsoft.JsonSystem.Text.Json
  • 谢谢,我已经在使用 Newtonsoft.Json
  • 为什么items 不是 JSON 数组?会让迭代更容易。
  • @phuzi、[JsonProperty("items")] public Dictionary&lt;string, Item&gt; Items { get; set; } 和瞧。无需更改原始Json。

标签: c# json asp.net-web-api


【解决方案1】:

我认为它不是很干净,但试试这个:

            var gameInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(data);
            var prop = gameInfo.Properties().Children().ToList()[0]["items"].ToArray();

            foreach (var item in prop)
            {
                if (item.Children().ToArray()[0]["itemShelf"].ToString() != "Liquor")
                    continue;
            }

【讨论】:

    【解决方案2】:

    使用 Linq 怎么样:

    var ItemsThatAreNotLiqour = content.Items.Where(item => item.itemShelf != "Liquor");
    

    你的课程应该是这样的(我猜这会以某种方式改变你的 json):

    public class Item
    {
        public string itemQuantity { get; set; }
        public string itemAmount { get; set; }
        public string itemShelf { get; set; }
    }
    
    public class Content
    {
        public ShopCart shopCart { get; set; }
        public Item[] items { get; set; }
    }
    

    【讨论】:

    • 我认为如果您将 Items 从和数组更改为 Dictionary&lt;string, Item&gt;,您可能不必更改 json。
    • Items 不是数组,你不能这样做。
    • @DhonJoe,Content 类到底是什么?
    • @Andrew 这是购物车的内容
    • 对,这很明显。这就是为什么我说完全正确,我期待一些定义。为什么不能遍历Items
    【解决方案3】:

    我的建议是JS把item1,item2,ite3转成数组item

    let items = [];
    for (var i = 1; i < 4; i++) {
        items.push(content.items['item' + i]);
    }
    

    您可以在 C# 中使用 LinQ

    bool anyLiquor = content.Items.Any(x => x.itemShelf == "Linquor");
    

    【讨论】:

    • 为什么是 JS ?这是关于 c#
    • 因为您需要转换为项目数组才能使用 LinQ。请查看 JSON
    猜你喜欢
    • 1970-01-01
    • 2017-03-17
    • 2022-07-12
    • 1970-01-01
    • 2021-08-20
    • 2017-12-04
    • 2013-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多