Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#——获取,然后比较JSON字符串中的值_C#_Json - Fatal编程技术网

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

C#——获取,然后比较JSON字符串中的值,c#,json,C#,Json,我需要从JSON字符串中提取值,以便比较它们。 我只需要验证它们是否有序(升序/降序)。 我要检查第一个和第二个“选择”并进行比较。 我没有比这更高级的了 编辑/更新: 如何在这种类型的查询中使用通配符(*)来跳过每个段 string one = (string)o[this.Context[*WILDCARD*]["cid1"]].ToString(); /* this works, but has too many []

我需要从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"
          }
        }
      }
    }
  }
}   

您遇到一些困难的原因可能是两个
“choicec”
属性在JSON层次结构中的深度不同。第一个在
“round2”
下面,而第二个不在下面。因此,简单的索引将不起作用

假设您能够使用,您的选项是:

  • 用于查找名为
    “choicec”
    的所有属性,并检查它们是否已订购:

        var obj = JObject.Parse(json);
        bool inOrder = obj.Descendants()
            .OfType<JProperty>()
            .Where(p => p.Name == "choicec")
            .Select(p => (int)p.Value)
            .IsOrdered();
    
    这里的
    是一个通配符,意思是“递归下降”

  • 使用以下按顺序排列的扩展名:

    公共静态类EnumerableExtensions
    {
    //取自http://stackoverflow.com/questions/19786101/native-c-sharp-support-for-checking-if-an-ienumerable-is-sorted
    公共静态布尔已排序(此IEnumerable集合,IComparer comparer=null)
    {
    if(集合==null)
    抛出新ArgumentNullException();
    比较器=比较器??比较器。默认值;
    使用(var enumerator=collection.GetEnumerator())
    {
    if(枚举数.MoveNext())
    {
    var previous=枚举数。当前值;
    while(枚举数.MoveNext())
    {
    var current=枚举数。当前值;
    如果(比较器比较(上一个,当前)>0)
    返回false;
    先前=当前;
    }
    }
    }
    返回true;
    }
    }
    
    听起来是个很酷的项目!有什么问题吗?我试过使用LINQ、Jtoken、Jpath和其他工具,但都失败或返回空。因此,我正在向社区伸出援助之手,尝试使用JSON.NET来实现这一点(在添加Nuget包后,您需要使用Newtonsoft.JSON
    ):
    dynamic d=JsonSerializer.Deserialize(您的jsonString)
    然后使用
    d.partner.campaign.round1.def123.community.choicec
    访问选项编号。为什么
    动态
    ?必须有一个具体的CALS。JSON中没有
    “cid1”
    属性。为了确认,您需要比较
    “choicec”:28
    “choicec”:33
    属性的值,以检查它们是否有序?惊人、彻底且有用。
        // 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();
    
    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;
        }
    }