Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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
Android 比较两个JSON对象_Android_Json_Gson - Fatal编程技术网

Android 比较两个JSON对象

Android 比较两个JSON对象,android,json,gson,Android,Json,Gson,我有一个类似JSON的对象 { key1:value1, key2:value2 key3:value3 } 我将把这个JSON内容写入文件 下一段时间,我将JSON对象作为 { key1:newValue1, key2:value2 key3:newValue3 } 我需要找出每个值之间的差异。并且需要将新的json写入文件 { key1:(value1- newValue1), key2:(value2 - value2) key3:(value3- ne

我有一个类似JSON的对象

{
key1:value1,
key2:value2
key3:value3
}
我将把这个JSON内容写入文件

下一段时间,我将JSON对象作为

{
    key1:newValue1,
    key2:value2
    key3:newValue3
    }
我需要找出每个值之间的差异。并且需要将新的json写入文件

{
key1:(value1- newValue1),
key2:(value2 - value2)
key3:(value3- newValue3)
}

我怎样才能做到。?请帮忙

下面的代码只是迭代测试对象的键,从文件中减去对象的值,然后将减去的结果返回到测试对象。。。然后可以保存,或者对testObject中的值执行任何您想执行的操作

        for(Iterator<String> iterator = testObject.keys(); iterator.hasNext();) {
            String key = iterator.next();

            try {
                int testValue = testObject.getInt(key);
                int fileValue = fileObject.getInt(key);
                int differenceValue = testValue - fileValue;
                testObject.put(key, differenceValue);
            } catch (JSONException e) {e.printStackTrace();}
        }
for(迭代器迭代器=testObject.keys();迭代器.hasNext();){
String key=iterator.next();
试一试{
int testValue=testObject.getInt(键);
int fileValue=fileObject.getInt(键);
int differenceValue=testValue-fileValue;
put(key,differenceValue);
}catch(JSONException e){e.printStackTrace();}
}

值将为int类型,您想将值更新为减法值,对吗?@Neo ya正确。任何数学运算。好的,我会给你写一些代码作为答案谢谢@Neo这是我的荣幸。这是你所期望的吗?