我正在开发一个c ++项目,我需要比较两个或多个Json字符串,它将作为函数中的参数传递给我,我必须相应地返回一个bool。我正在使用Jsoncpp,但我无法比较两个Json数据的全部。我想知道循环键和值的最佳过程,并使用另一个json字符串的相应值检查值(两个String都将传递给函数,并将使用jsoncpp的reader.parse()解析,然后我需要比较它们并返回bool值)。有人可以帮我这个吗?先感谢您。 我被困的地方:
class test {
public:
static bool isequalstring(const std::string &item1, const std::string
&item2, const std::string &temp) {
Document d1;
d1.Parse(item1.c_str());
Document d2;
d2.Parse(item2.c_str());
Document d3;
d3.Parse(temp.c_str());
bool matched = true;
//itr= iterate through the third json to get the keys and match the keys in first and second
for (auto itr = d3.MemberBegin(); itr != d3.MemberEnd(); itr++) {
if (d1.HasMember(itr->name) && d2.HasMember(itr->name)) { // if the member doesn't exist in both, break
if (d1[itr->name] != d2[itr->name]) {
// value doesn't match, then break
matched = false;
break;
}
} else {
matched = false;
break;
}
}
return matched;
}
};
bool testDeepNestedJson_should_succeed(){
bool expectedTestResult = true;
bool testResult;
// Input 1 JSON Object
const char* input1 = "{\"array\":[1,2,3],\"boolean\":true,\"null\":null,\"number\":123,\"object\":{\"a\":\"b\",\"c\":\"d\",\"e\":\"f\"},\"string\":\"Hello World\",\"object_array\":[{\"key\":\"value1\"},{\"key\":\"value2\"},{\"key\":\"value3\"}],\"deep_nested_array\":[{\"object_array\":[{\"key\":\"value1\"},{\"key\":\"value2\"},{\"key\":\"value3\"}]},{\"object_array\":[{\"key\":\"value4\"},{\"key\":\"value5\"},{\"key\":\"value6\"}]}]}";
const char* input2 = "{\"array\":[1,2,3],\"justsomedata\":true,\"boolean\":true,\"null\":null,\"object\":{\"a\":\"b\",\"c\":\"d\",\"e\":\"f\"},\"number\":123,\"object_array\":[{\"key\":\"value1\"},{\"key\":\"value2\"},{\"key\":\"value3\"}],\"deep_nested_array\":[{\"object_array\":[{\"key\":\"value1\"},{\"key\":\"value2\"},{\"key\":\"value3\"}]},{\"object_array\":[{\"key\":\"value4\"},{\"key\":\"value5\"},{\"key\":\"value6\",\"ignoreme\":12346}]}],\"string\":\"Hello World\"}";
const char* stencil = "{\"array\":[null],\"boolean\":null,\"null\":null,\"object\":{\"a\":null,\"c\":null,\"e\":null},\"number\":null,\"object_array\":[{\"key\":null}],\"deep_nested_array\":[{\"object_array\":[{\"key\":null}]}],\"string\":null}";
testResult = test::isequalstring(input1, input2, stencil);
if(testResult != expectedTestResult){
std::cout<<"testDeepNestedJson_should_succeed:"<<std::endl;
std::cout<<"Item1:"<<input1<<std::endl;
std::cout<<"Item2:"<<input2<<std::endl;
std::cout<<"Stencil:"<<stencil<<std::endl;
std::cout<<"Test Failed result is: False expected was: True"<<std::endl;
return false;
}
std::cout<<"PASSED: testDeepNestedJson_should_succeed"<<std::endl;
return true;
}
int main() {
testDeepNestedJson_should_succeed();
return 0;
}
答案 0 :(得分:0)
使用RapidJSON,代码将是这样的
Note: the configuration refers to the unknown class 'com.google.android.gms.common.internal.safeparcel.SafeParcelable'
Note: the configuration refers to the unknown class 'android.app.OnActivityPausedListener'
Note: the configuration refers to the unknown class 'com.mopub.nativeads.CustomEventNative'
Maybe you meant the fully qualified name 'com.google.android.gms.ads.mediation.customevent.CustomEventNative'?
Note: the configuration refers to the unknown class 'com.mopub.nativeads.CustomEventRewardedAd'
Note: the configuration refers to the unknown class 'Object'
Maybe you meant the fully qualified name 'java.lang.Object'?
答案 1 :(得分:0)
您可以使用name()
迭代root2
,获取密钥名称,使用operator[]
访问root
和root1
中包含该名称的值,然后进行比较他们使用operator==
:
for (auto it = root2.begin(); it != root2.end(); ++it) {
auto name = it.name();
if (root[name] != root1[name])
return false;
}
return true;
<子>顺便说一句。您将item1解析为root,将item2解析为root1,将temp解析为root2。你可以在命名方面更加一致。