如何在 C# 中过滤 JSON 数组 [英] How to filter JSON array in C#

查看:52
本文介绍了如何在 C# 中过滤 JSON 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了很多时间来为我的问题找到解决方案.

I have spent a lot of time to find a solution for my problem.

在这个例子中,我在 SetNavRecords 数组中有 2 条记录.第一个是 "Artikelnummer" : "21700" 第二个是 "Artikelnummer" : "21701"

In this example, I have 2 records in SetNavRecords array. The first one is "Artikelnummer" : "21700" and the second one is "Artikelnummer" : "21701"

每条记录都有一个数组OfflineVerkaufspreis".

Each record have an array "OfflineVerkaufspreis".

对我来说重要的是OfflineVerkaufspreis"中的Location_Code"字段我只需要一个过滤后的位置代码的孔信息.

Important is for me the field "Location_Code" in "OfflineVerkaufspreis" I only need the hole informations for one filtered Location Code.

如何选择一个位置代码的数据,例如MH"?

How can I select the data for one Location Code, for example "MH"?

我使用 C# 和 Newtonsoft 类进行 JSON 解析.

I am using C# and the Newtonsoft Class for JSON parsing.

我用 LINQ 尝试了一些版本,但没有成功.

I have tried some versions with LINQ but without success.

{ "SetNavRecords" : [ { "Artikelbeschreibung" : "Trikot "Home" 2012/2013",
    "Artikelbeschreibung2" : "weiß",
    "Artikelnummer" : "21700",
    "Artikelrabattgruppe" : "MERCH",
    "Gutschein" : false,
    "MwStProduktgruppe" : "VOLLNEU",
    "OfflineVerkaufspreis" : [ { "Allow_Line_Discount" : true,
          "Date" : "2014-05-16T00:00:00",
          "Item_No" : "21700",
          "Location_Code" : "BP",
          "Unit_Price" : 5.0
        },
        { "Allow_Line_Discount" : true,
          "Date" : "2014-05-16T00:00:00",
          "Item_No" : "21700",
          "Location_Code" : "MH",
          "Unit_Price" : 5.0
        },
        { "Allow_Line_Discount" : true,              
          "Date" : "2014-05-16T00:00:00",
          "Item_No" : "21700",
          "Location_Code" : "RY",
          "Unit_Price" : 5.0
        }
      ]
  },
  { "Artikelbeschreibung" : "Autogrammtrikot 2012/2013",
    "Artikelbeschreibung2" : "weiß",
    "Artikelnummer" : "21701",
    "Artikelrabattgruppe" : "MERCH",
    "Gutschein" : false,
    "MwStProduktgruppe" : "VOLLNEU",
    "OfflineVerkaufspreis" : [ { "Allow_Line_Discount" : false,
          "Date" : "2014-05-16T00:00:00",
          "Item_No" : "21701",
          "Location_Code" : "BP",
          "Unit_Price" : 69.99
        },
        { "Allow_Line_Discount" : false,
          "Date" : "2014-05-16T00:00:00",
          "Item_No" : "21701",
          "Location_Code" : "MH",
          "Unit_Price" : 69.99
        },
        { "Allow_Line_Discount" : false,
          "Date" : "2014-05-16T00:00:00",
          "Item_No" : "21701",
          "Location_Code" : "RY",
          "Unit_Price" : 69.99
        }
      ]
  }

] }

这是我的问题:

var tmpResult = JObject.Parse(File.ReadAllText(FileName));
var resultObject = tmpResult["SetNavRecords"]
      .Values("OfflineVerkaufspreis")
      .Values<JObject>()                                   
      .Where(n => n["Location_Code"].Value<string>() == "MH"); 

过滤器工作正常,但 tmpResult 中的数据不完整.我只在OfflineVerkaufspreis"中获取数据.我也需要根数据.

The filter works fine but the data is incomplete in tmpResult. I only get the data in "OfflineVerkaufspreis". I need the root data too.

有人有想法吗?

谢谢!

推荐答案

通过切换到将 json 序列化为对象,这感觉就像是一项可以变得更容易的工作.要将 json 反序列化为您可以使用的对象:

This feels like a job that could be made a little easier by switching to serialization of the json into objects. To deserialize the json into an object you could use:

RootObject obj = JsonConvert.DeserializeObject<RootObject>(jsonString);

同样,您可以使用以下方法将对象重新转换为 json:

likewise, you can turn your object back into json using:

string jsonString = JsonConvert.SerializeObject(RootObject);

基于您在问题中提供的json的对象结构如下:

The structure of your object based on the json you provided in the question is such:

public class OfflineVerkaufsprei
{
    public bool Allow_Line_Discount { get; set; }
    public string Date { get; set; }
    public string Item_No { get; set; }
    public string Location_Code { get; set; }
    public double Unit_Price { get; set; }
}

public class SetNavRecord
{
    public string Artikelbeschreibung { get; set; }
    public string Artikelbeschreibung2 { get; set; }
    public string Artikelnummer { get; set; }
    public string Artikelrabattgruppe { get; set; }
    public bool Gutschein { get; set; }
    public string MwStProduktgruppe { get; set; }
    public List<OfflineVerkaufsprei> OfflineVerkaufspreis { get; set; }
}

public class RootObject
{
    public List<SetNavRecord> SetNavRecords { get; set; }
}

然后您可以通过说:

for each(SetNavRecord i in obj.SetNavRecords)
{
    // do something to the record
}

这篇关于如何在 C# 中过滤 JSON 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆