码迷,mamicode.com
首页 > Web开发 > 详细

String转json,多层json解析,并使用JsonElement对比两个json对象的值是否相等

时间:2020-05-14 11:01:40      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:one   数组类   test   需要   dep   导入   artifact   str   stat   

导入需要的jar包:

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.2</version>
</dependency>

示例:

public class JsonTest {


    public static void main(String[] args) {
        String rightAnswer = "{\"unit\":\"单位\",\"year\":\"2018\",\"form\":[{\"subject\":\"数学\",\"summary\":[11,12,13],\"score\":\"88.00\"},{\"subject\":\"语文\",\"summary\":[13,11],\"score\":\"99.00\"},{\"subject\":\"化学\",\"summary\":[],\"score\":\"77.00\"},{\"subject\":\"政治\",\"summary\":[1],\"score\":\"100.00\"}],\"people\":\"花花\"}";
        String answer = "{\"unit\":\"单位\",\"year\":\"2018\",\"form\":[{\"subject\":\"数学\",\"summary\":[11,12,13],\"score\":\"88.00\"},{\"subject\":\"语文\",\"summary\":[13,11],\"score\":\"99.00\"},{\"subject\":\"化学\",\"summary\":[],\"score\":\"77.00\"},{\"subject\":\"政治\",\"summary\":[1],\"score\":\"100.00\"}],\"people\":\"草草\"}";

        String a = "{\"error\":[10,14],\"imgId\":[1]}";
        String b = "{\"error\":[14,10],\"imgId\":[]}";
        
        JsonParser jsonParser = new JsonParser();
        System.out.println("=====" + JsonTest.test(jsonParser.parse(answer), jsonParser.parse(rightAnswer)));
    }

    public static Boolean test(JsonElement answerElement, JsonElement rightAnswerElement) {
        if (rightAnswerElement.isJsonArray()) {
            JsonArray rightAnswerJsonArray = rightAnswerElement.getAsJsonArray();
            if (rightAnswerJsonArray.size() == answerElement.getAsJsonArray().size()) {
            //数组类型顺序会影响取值,所以每个数组都要全部匹配验证另外一个对象的数组。
            //例如:[10,14]和[14,10]
                for (int i = 0; i < rightAnswerJsonArray.size(); i ++) {
                    boolean flag = false;
                    JsonArray answerJsonArray = answerElement.getAsJsonArray();
                    for (int j = 0; j < answerJsonArray.size(); j ++) {
                        if (JsonTest.test(answerElement.getAsJsonArray().get(j), rightAnswerJsonArray.get(i))) {
                            flag = true;
                            break;
                        }
                    }
                    if (!flag) {
                        return false;
                    }
                }
            } else {
                return false;
            }
        } else if (rightAnswerElement.isJsonObject()) {
            JsonObject rightAnswerJsonObject = rightAnswerElement.getAsJsonObject();
            Set<String> keys = rightAnswerJsonObject.keySet();
            for (String key : keys) {
                if(!JsonTest.test(answerElement.getAsJsonObject().get(key), rightAnswerJsonObject.get(key))) {
                    return false;
                }
            }
        } else if (rightAnswerElement.isJsonPrimitive()) {
            String rightAnswer = rightAnswerElement.getAsString();
            String answer = answerElement.getAsString();
            System.out.println("rightAnswer = " + rightAnswer + ", answer = " + answer);
            if (!rightAnswer.equals(answer)) {
                return false;
            }
        }
        return true;
    }
}

String转json,多层json解析,并使用JsonElement对比两个json对象的值是否相等

标签:one   数组类   test   需要   dep   导入   artifact   str   stat   

原文地址:https://www.cnblogs.com/zys-blog/p/12886666.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!