检测在C#两个JSON文件之间的差异 [英] Detect differences between two json files in c#

查看:93
本文介绍了检测在C#两个JSON文件之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果我有以下的JSON文本:

  object1 {
字段1:值1;
场2:值2;
场3:值3;
}

object1 {
字段1:值1;
场2:newvalue2;
场3:值3;
}



我需要在C#中的东西,读取该文件并显示差异。也就是说,它可以返回以下对象:

 差异{
object1:{场:场,OLD_VALUE:值2,NEW_VALUE :newvalue2}
}

有一些API或建议这样做。

解决方案

我建议你使用的弱类型的JSON序列化并编写使用常规的的JSONObject 是这样的:

 字符串JsonDifferenceReport(字符串对象名,
的JSONObject第一,
的JSONObject秒)
{
如果(String.IsNullOrEmpty(对象名))
抛出新ArgumentNullException (对象名);
如果(空==第一)
抛出新的ArgumentNullException(第一);
如果(空==秒)
抛出新的ArgumentNullException(第二);
名单,LT;弦乐> allKeys =新的List<串GT;();
的foreach(在first.Keys字符串键)
如果(allKeys.Any(X =>!X.Equals(键)))allKeys.Add(密钥);
的foreach(在second.Keys字符串键)
如果(allKeys.Any(X =>!X.Equals(键)))allKeys.Add(密钥);
字符串结果=的String.Empty;
的foreach(在allKeys字符串键)
{
JsonValue V1 =第[关键]
JsonValue V1 =秒[关键]
如果(((空== V1)!=(空== V2))||!v1.Equals(V2))
{
如果(String.IsNullOrEmpty(结果))
{
结果=的区别:{\\\

}
结果+ =\t+ +的objectName:{\\\
+ =\t\tfield:
+业绩键+,\\\

结果+ =\t\toldvalue:+(空== V1)? 空:v1.ToString()+,\\\
;
结果+ =\t\tnewvalue:+(空== V2)? 空:v2.ToString()+\\\
;
结果+ =\t} \\\

}
}
如果
{
结果+ =} \\\
(String.IsNullOrEmpty(结果)!);
}
返回结果;
}

您选择是否得到报告递归里面 JsonValue v1和v2,而不是仅仅使用他们的字符串表示我在这里做的。



如果你想去递归的,它可能会改变上述像这样的:

  IF((空== V1)||(v1.JsonType == JsonType.JsonPrimitive)
||(空== V2)||(v2.JsonType == JsonType.JsonPrimitive))
{
结果+ =\t\tfield:+按键+,\\\

结果+ =\t\toldvalue:+(空== V1)? 空:v1.ToString()+,\\\
;
结果+ =\t\tnewvalue:+(空== V2)? 空:v2.ToString()+\\\
;
}
,否则
{
+结果JsonDifferenceReport(键,V1,V2);
}



-Jesse


For example, if I have the following json texts:

 object1{
     field1: value1;
     field2: value2;
     field3: value3;
 }

 object1{
     field1: value1;
     field2: newvalue2;
     field3: value3;
 }

I need something in c# that reads that files and shows the difference. i.e. it can return the following object:

differences {
    object1: { field: field2, old_value: value2, new_value: newvalue2}
}

Is there some API or suggestions to do this?

解决方案

I recommend you use Weakly-Typed JSON Serialization and write a routine that uses JsonObject like this:

String JsonDifferenceReport(String objectName,
                            JsonObject first,
                            JsonObject second)
{
  if(String.IsNullOrEmpty(objectName))
    throw new ArgumentNullException("objectName");
  if(null==first)
    throw new ArgumentNullException("first");
  if(null==second)
    throw new ArgumentNullException("second");
  List<String> allKeys = new List<String>();
  foreach(String key in first.Keys)
    if (!allKeys.Any(X => X.Equals(key))) allKeys.Add(key);
  foreach(String key in second.Keys)
    if (!allKeys.Any(X => X.Equals(key))) allKeys.Add(key);
  String results = String.Empty;
  foreach(String key in allKeys)
  {
    JsonValue v1 = first[key];
    JsonValue v1 = second[key];
    if (((null==v1) != (null==v2)) || !v1.Equals(v2))
    {
      if(String.IsNullOrEmpty(results))
      {
         results = "differences: {\n";
      }
      results += "\t" + objectName + ": {\n";
      results += "\t\tfield: " + key + ",\n";
      results += "\t\toldvalue: " + (null==v1)? "null" : v1.ToString() + ",\n";
      results += "\t\tnewvalue: " + (null==v2)? "null" : v2.ToString() + "\n";
      results += "\t}\n";
    }
  }
  if(!String.IsNullOrEmpty(results))
  {
    results += "}\n";
  }
  return results;
}

Your choice whether to get reports recursively inside JsonValue v1 and v2, instead of just using their string representation as I did here.

If you wanted to go recursive, it might change the above like this:

  if ((null==v1) || (v1.JsonType == JsonType.JsonPrimitive)
   || (null==v2) || (v2.JsonType == JsonType.JsonPrimitive))
  {
    results += "\t\tfield: " + key + ",\n";
    results += "\t\toldvalue: " + (null==v1) ? "null" : v1.ToString() + ",\n";
    results += "\t\tnewvalue: " + (null==v2) ? "null" : v2.ToString() + "\n";
  }
  else
  {
    results + JsonDifferenceReport(key, v1, v2);
  }

-Jesse

这篇关于检测在C#两个JSON文件之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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