Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

551
Views
Java 8: trabajar con JsonObject (javax.json): ¿cómo determinar si un nodo tiene nodos secundarios?

Acabo de empezar a usar el paquete javax.json. Sé cómo extraer valores del objeto JSON en Java 8 si conozco la estructura de mi cadena JSON. Pero, ¿qué hay de las cadenas de las que no conozco la estructura? La pregunta: ¿cómo determinar si un nodo tiene nodos secundarios?

Para leer valores, simplemente necesito usar métodos "get*"; funciona bien, pero no hay ningún método como "checkIfArray" o "checkIfObject" para verificar si puedo usar métodos como "getString"...

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

En el paquete javax.json , ambos tipos disponibles admiten comprobaciones de vacío porque ambos implementan interfaces java.collection :

JsonObject es un java.util.Map<String, JsonValue> . Como resultado, uno puede verificar si dicho objeto contiene algún valor simplemente llamando al método isEmpty() .

JsonArray es un java.util.List<JsonValue> . Como resultado, uno, nuevamente, puede verificar si la matriz está vacía llamando al método isEmpty() en ella.

Para atravesar un árbol de JsonStructure s mientras marca todos los nodos vacíos, puede usar este método auxiliar:

 boolean isValueEmpty(JsonValue v) { if (v == null) { return true; // or you may throw an exception, for example } switch(v.getValueType()) { case NULL: return true; // same as with Java null, we assume that it is Empty case ARRAY: return ((JsonArray) v).isEmpty(); // additionally, you can say that empty array is array with only empty elements // this means a check like this: // return ((JsonArray v).stream().allMatch(isValueEmpty); // recurse case OBJECT: return ((JsonObject v).isEmpty(); case STRING: // optionally: if you want to treat '' as empty return ((JsonString v).getString().isEmpty(); default: return false; // to my knowledge, no other Json types can be empty } }
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!