如何过滤JSON字符串

时间:2019-05-02 18:49:53

标签: c# arrays json json.net

在我的应用程序方法中,使用称为GetAllProvince()的方法按如下方式填充JSON字符串,请注意GetAllProvince()是Web方法

public string GetAllProvince()
{
    NccEntity nccEnti = getLtsLobbyInstance();
    DataTable dt = nccEnti.GetAllBranchesAndRegions();
    JavaScriptSerializer jsSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
    return jsSerializer.Serialize(dt.Rows.Cast<DataRow>().GroupBy(row => row["RegionId"], row => row, (key, values) => new ProvinceWithBranches
    {
        Value = key.ToString(),
        Province = values.Select(row => row["RegionName"].ToString()).FirstOrDefault(),
        Branches = values.Select(row => new BranchItem
        {
            Value = row["BranchId"].ToString(),
            Province = row["BranchName"].ToString(),
            IsValid = row["IsTestBranch"].ToString()
        })
    }));
}

名为ProvinceWithBranches

的类
public class ProvinceWithBranches
{
    public string Value;
    public string Province;
    public IEnumerable<BranchItem> Branches;
}

名为BranchItem

的类
public class BranchItem
{
    public string Value;
    public string Province;
    public string IsValid;
}

上述生成JSON字符串的方法如下,

[
  {
    "Value": "21",
    "Province": "Default",
    "Branches": [
      {"Value": "1108", "Province": "davie", "IsValid": "False"},
      {"Value": "1107", "Province": "Ab area109", "IsValid": "False"},
      {"Value": "1105", "Province": "Hollywood", "IsValid":"False"},
      {"Value": "1105", "Province": "Ft. Lauderdale", "IsValid":"False"}
    ]
  },
  {
    "Value": "17",
    "Province": "East",
    "Branches": [
      {"Value": "212", "Province": "area109", "IsValid": "False"},
      {"Value": "219", "Province": "lauderdale", "IsValid": "False"}
    ]
  },
  {
    "Value": "24",
    "Province": "East11",
    "Branches": [
      {"Value": "211", "Province": "area108", "IsValid": "False"},
      {"Value": "218", "Province": "area109", "IsValid": "False"},
      {"Value": "1102", "Province": "area999", "IsValid": "False"}
    ]
  },
  {
    "Value": "25",
    "Province": "N25",
    "Branches": [
      {"Value": "213", "Province": "davie", "IsValid": "False"},
      {"Value": "220", "Province": "area999", "IsValid": "False"},
      {"Value": "220", "Province": "areda999", "IsValid": "False"}
    ]
  }
]

现在我的要求是我需要将参数作为GetAllProvince(string searcKey)传递给该方法,并基于searcKey仅应过滤并返回相关数据,

作为示例1 , 应该返回以下searcKey = Default个结果时(所有匹配的案例都应返回根级别或分支级别)

[
  {
    "Value": "21",
    "Province": "Default",
    "Branches": [
      {"Value": "1108", "Province": "davie", "IsValid": "False"},
      {"Value": "1107", "Province": "Ab area109", "IsValid": "False"},
      {"Value": "1105", "Province": "Hollywood", "IsValid":"False"},
      {"Value": "1105", "Province": "Ft. Lauderdale", "IsValid":"False"}
    ]
  }
]

示例2 , 应该返回以下searcKey = area109个结果时(所有匹配的案例都应返回根级别或分支级别)

[
  {
    "Value": "21",
    "Province": "Default",
    "Branches": [
      {"Value": "1107", "Province": "Ab area109", "IsValid": "False"},
    ]
  },
  {
    "Value": "17",
    "Province": "East",
    "Branches": [
      {"Value": "212", "Province": "area109", "IsValid": "False"}
    ]
  },
  {
    "Value": "24",
    "Province": "East11",
    "Branches": [
      {"Value": "218", "Province": "area109", "IsValid": "False"}
    ]
  }
]

我该怎么做,请帮助我这两天的工作量。非常感谢。

2 个答案:

答案 0 :(得分:0)

我不会过滤json字符串。我会将json字符串反序列化为ProvinceWithBranches的列表,然后执行循环以将IEnumerable BranchItem类读入某些内容,然后可以对此执行linq查询。或内循环(如果您愿意)。我认为您几乎可以满足需要。

或者为什么不对数据表进行搜索。

答案 1 :(得分:0)

反序列化为Province of Branch的数组,然后

    private  IEnumerable<ProvinceWithBranches> Get(IEnumerable<ProvinceWithBranches> source,string filter)
          {
             var firstConditionResults= source.Where(p => p.Province == filter);
              var secondConditionResults = source.Where(p => p.Province  !=filter).Select(p=>new ProvinceWithBranches{
Value = p.Value,
Province = p.Province,
Branches = p.Branches.Where(branch=>branch.Province==filter) } );

              return firstConditionResults.Concat(secondConditionResults);
          }