C# - 获取,然后比较JSON字符串中的值

Mic*_*itt 6 c# json

我需要从JSON字符串中提取值,以便我可以比较它们.我只需要验证它们是否有序(升序/降序).我打算检查第一个和第二个"选择"并进行比较.我没有更高级的东西.

编辑/更新:我如何在这种类型的查询中使用通配符(*)来跳过每个段?

               string one = (string)o[this.Context[*WILDCARD*]["cid1"]].ToString();


           /* this works, but has too many []
           string one = (string)o[this.Context["partner"]]
               [this.Context["campaign"]]
               [this.Context["segment1"]]
               [this.Context["segment2"]]
               [this.Context["qid2"]]
               ["community"]
               [this.Context["cid1"]].ToString();
           */


{
  "partner": {
    "campaign": {
      "round1": {
        "round2": {
          "def123": {
            "community": {
              "choicec": 28
            },
            "user": {
              "choice": "choicec",
              "writeDateUTC": "2015-06-15T17:21:59Z"
            }
          }
        },
        "abc321": {
          "community": {
            "choicec": 33
          },
          "user": {
            "choice": "choicec",
            "writeDateUTC": "2015-06-15T17:21:59Z"
          }
        }
      }
    }
  }
}   
Run Code Online (Sandbox Code Playgroud)

dbc*_*dbc 1

您遇到一些困难的原因可能是这两个"choicec"属性在 JSON 层次结构中的深度不同。第一个在下面"round2",而第二个不在下面。因此,直接建立索引是行不通的。

假设您能够使用Json.NET,您的选择是:

  1. 用于Descendants查找所有命名的属性"choicec"并检查它们是否有序:

        var obj = JObject.Parse(json);
        bool inOrder = obj.Descendants()
            .OfType<JProperty>()
            .Where(p => p.Name == "choicec")
            .Select(p => (int)p.Value)
            .IsOrdered();
    
    Run Code Online (Sandbox Code Playgroud)
  2. 如果层次结构中碰巧存在与查询不相关的其他属性,则SelectTokensJsonPath 通配符一起使用可将搜索限制为 JSON 的一部分:"choicec"

        // Find all properties named "choicec" under "community" recursively under "campaign" under "partner".
        bool inOrder = obj.SelectTokens("partner.campaign..community.choicec")
            .Select(o => (int)o)
            .IsOrdered();
    
    Run Code Online (Sandbox Code Playgroud)

    ..是一个通配符,意思是“递归下降”。

使用Mikkel R. Lund提出的这个问题的以下IsOrdered扩展:

public static class EnumerableExtensions
{
    // Taken from http://stackoverflow.com/questions/19786101/native-c-sharp-support-for-checking-if-an-ienumerable-is-sorted
    public static bool IsOrdered<T>(this IEnumerable<T> collection, IComparer<T> comparer = null)
    {
        if (collection == null)
            throw new ArgumentNullException();
        comparer = comparer ?? Comparer<T>.Default;

        using (var enumerator = collection.GetEnumerator())
        {
            if (enumerator.MoveNext())
            {
                var previous = enumerator.Current;

                while (enumerator.MoveNext())
                {
                    var current = enumerator.Current;

                    if (comparer.Compare(previous, current) > 0)
                        return false;

                    previous = current;
                }
            }
        }

        return true;
    }
}
Run Code Online (Sandbox Code Playgroud)