Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

261
Visualizações
JSONObject.similar(JSONObject) what does this really compare?

I was looking for a way to compare two JSONObjects besides using

JSONObject.toString().equals(JSONObject.toString())

and came across this similar method but it's really vague about what it compares.

"Determine if two JSONObjects are similar. They must contain the same set of names which must be associated with similar values." Link

What is that supposed to mean? How similar? Exactly the same?

I searched really hard and couldn't find anything that could clear it up.

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Here's the source for JSONObject.similar():

/**
 * Determine if two JSONObjects are similar.
 * They must contain the same set of names which must be associated with
 * similar values.
 *
 * @param other The other JSONObject
 * @return true if they are equal
 */
public boolean similar(Object other) {
    try {
        if (!(other instanceof JSONObject)) {
            return false;
        }
        if (!this.keySet().equals(((JSONObject)other).keySet())) {
            return false;
        }
        for (final Entry<String,?> entry : this.entrySet()) {
            String name = entry.getKey();
            Object valueThis = entry.getValue();
            Object valueOther = ((JSONObject)other).get(name);
            if(valueThis == valueOther) {
                continue;
            }
            if(valueThis == null) {
                return false;
            }
            if (valueThis instanceof JSONObject) {
                if (!((JSONObject)valueThis).similar(valueOther)) {
                    return false;
                }
            } else if (valueThis instanceof JSONArray) {
                if (!((JSONArray)valueThis).similar(valueOther)) {
                    return false;
                }
            } else if (valueThis instanceof Number && valueOther instanceof Number) {
                if (!isNumberSimilar((Number)valueThis, (Number)valueOther)) {
                    return false;
                };
            } else if (!valueThis.equals(valueOther)) {
                return false;
            }
        }
        return true;
    } catch (Throwable exception) {
        return false;
    }
}

Essentially similar() recursively compares the names and values of the JSONObjects and returns true if they're the same. First, it checks if the keySets are equal, then moves on to the values. It recursively checks each value in the keySet to see if they are equal in each JSONObject. If not, it returns false.

about 4 years ago · Santiago Trujillo Relatório

0

The description you found relates to method JSONObject.similar(), which compares if two JSON objects are the same, but having perhaps a different order of its attributes.

The equals() will compare each string caracter, one-by-one checking if it is the same, having the same order.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda