如何从json响应中选择项目C#

时间:2019-06-26 10:11:13

标签: c# json json.net

我正在尝试创建通配符搜索功能。 我有一个json响应,其中包含用户名。我必须搜索te *之类的用户,因此它将显示相应的用户名。 像test1,test2 我用来获取响应的以下代码

 var JSONResponse = await SendGraphRequest("/users/", null, null, HttpMethod.Get);

我尝试了以下代码,并尝试仅在图形中进行过滤

我尝试仅在图形中过滤

  var JSON = await SendGraphRequest("/users/", $"$filter=startswith(givenname,'b')", null, HttpMethod.Get);
                var graphUserResponse2 = JsonConvert.DeserializeObject<GraphUserResponseMapping>(JSON);

所以我想尝试使用用户名进行过滤,而不是使用给定名称。

我正在使用newtonsoft解析json,但是很难在列表中获取用户名,然后我将应用通配符搜索。但是问题是如何获取用户名并存储在列表中?

以下是json响应

{
   "odata.metadata": "test",
   "odata.nextLink":"test",
   "value": [
      {
      "odata.type": "Microsoft.DirectoryServices.User",
      "objectType": "User",
      "signInNames": [
                {
                  "type": "emailAddress",
                  "value": "test1@gmail"
                },
                {
                  "type": "username",
                  "value": "Test1"
                }
        ],
    "personId": "1"
    },
    {
        "odata.type": "Microsoft.DirectoryServices.User",
         "objectType": "User",
        "signInNames": [
            {
            "type": "emailAddress",
            "value": "test2@gmail.com"
             },
             {
              "type": "username",
              "value": "Test2"
              }
          ],
        "personId": "2"
        }
        ]
 }

TIA

1 个答案:

答案 0 :(得分:1)

罗杰!

您可以使用Class ex:

YourClassName.cs

此类内的代码

    public class SignInName
{
    public string Type { get; set; }
    public string Value { get; set; }
}

public class Value
{
    [JsonProperty(PropertyName = "odata.type")]
    public string OdataType { get; set; }
    public string ObjectType { get; set; }
    public List<SignInName> SignInNames { get; set; }
    public string PersonId { get; set; }
}

public class YourClassName
{
    [JsonProperty(PropertyName = "odata.metadata")]
    public string OdataMetadata { get; set; }

    [JsonProperty(PropertyName = "odata.nextLink")]
    public string OdataNextLink { get; set; }
    public List<Value> Value { get; set; }
}

因此您可以搜索用户名并将其放入列表中。

例如:

           List<string> userNameList = new List<string>();

        var json = "{ \"odata.metadata\": \"test\", \"odata.nextLink\":\"test\", \"value\": [ { \"odata.type\": \"Microsoft.DirectoryServices.User\", \"objectType\": \"User\", \"signInNames\": [ { \"type\": \"emailAddress\", \"value\": \"test1@gmail\" }, { \"type\": \"username\", \"value\": \"Test1\" } ], \"personId\": \"1\" }, { \"odata.type\": \"Microsoft.DirectoryServices.User\", \"objectType\": \"User\", \"signInNames\": [ { \"type\": \"emailAddress\", \"value\": \"test2@gmail.com\" }, { \"type\": \"username\", \"value\": \"Test2\" } ], \"personId\": \"2\" } ] }";

        var yourClassName = JsonConvert.DeserializeObject<YourClassName>(json);

        foreach (var value in yourClassName.Value)
        {
            userNameList.AddRange(value.SignInNames.Where(x => x.Type == "username").Select(x => x.Value).ToList());
        }