GSON: How to get a case insensitive element from Json?

前端 未结 3 1932
北海茫月
北海茫月 2020-12-06 11:06

Code shown below works well when JSON object contains jsonKey as it was passed to the method. I wonder ... if there is a way to get a value assigne

3条回答
  •  天命终不由人
    2020-12-06 11:34

    I faced the similar issue. I did this to get around the issue. (Replaced all the keys with their corresponding lowercase version and had all lower case fields in matching class). Hope this helps.

     input = input.replaceAll("\\s","");
            Matcher m = Pattern.compile("\"\\b\\w{1,}\\b\"\\s*:").matcher(input);
            StringBuilder sanitizedJSON = new StringBuilder();
            int last = 0;
            while (m.find()) {
                sanitizedJSON.append(input.substring(last, m.start()));
                sanitizedJSON.append(m.group(0).toLowerCase());
                last = m.end();
            }
            sanitizedJSON.append(input.substring(last));
    
            input = sanitizedJSON.toString();
    

提交回复
热议问题