If the array is defined in the file but is empty, like:

...
"kl":[]
...

Then getJSONArray("kl") will return an empty array, but the object is not null. Then, if you do this:

kl = c.getJSONArray("kl");
if(kl != null){
   klassenID[i] = kl.getJSONObject(0).getString("id");
}

kl is not null and kl.getJSONObject(0) will throw an exception - there is no first element in the array.

Instead you can check the length(), e.g.:

kl = c.getJSONArray("kl");
if(kl != null && kl.length() > 0 ){
   klassenID[i] = kl.getJSONObject(0).getString("id");
}
Answer from T.Gounelle on Stack Overflow
🌐
GitHub
github.com › eclipse-vertx › vert.x › pull › 2925 › files
provide empty object/array instance by MaiCw4J · Pull Request #2925 · eclipse-vertx/vert.x
import java.util.stream.StreamSupport; Expand All · @@ -43,6 +44,9 @@ public class Json { public static ObjectMapper mapper = new ObjectMapper(); public static ObjectMapper prettyMapper = new ObjectMapper(); private static final JsonObject EMPTY_JSON_OBJECT = new JsonObject(Collections.emptyMap()); private static final JsonArray EMPTY_JSON_ARRAY = new JsonArray(Collections.emptyList()); static { // Non-standard JSON but we allow C style comments in our JSON ·
Author: eclipse-vertx
🌐
GitHub
github.com › FasterXML › jackson-databind › issues › 2752
ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT does not convert empty JSON array to null value · Issue #2752 · FasterXML/jackson-databind
June 9, 2020 - However empty JSON array [] is neither set to null into JSON-array-to-JAVA-collection mapping nor JSON-array-to-JAVA-array.
Author: FasterXML
🌐
Tabnine
tabnine.com › home › code library
Code Library - Tabnine
July 25, 2024 - The Tabnine Code Library is no longer available — but you can still get the answers and suggestions you need from our AI code assistant. Get started in minutes with our free version right in your IDE, or try Tabnine Pro free for 90 days.
Top answer
1 of 2
1

From your JSON data, it seem skills is an array of objects.

"skills": [],
 "skills": [
   "skill_1": {},
   "skill_2": {}
 ]

But your Java code defines it as Map:

public Map<String, Skill> skills;

That's why you got an exception when trying to convert the array to a map directly. If you can't change the POJO's Profile, you should have an intermediate step to convert the list to a Map.

public class SkillsMapDeserializer extends JsonDeserializer<Map<String, Skill>> {
    @Override
    public Map<String, Skill> deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        final List<Map<String,Skill>> skills = jsonParser.readValueAs(new TypeReference<List<Map<String,Skill>>>>() {
        });
        return functionConvertListToMapWithParam(skills);
    }
}
2 of 2
-1

skills is not a map. it should be list of objects. try to modify your POJO like below:-

public class Profile {

    @JsonProperty("name")
    private String name;
    @JsonProperty("age")
    private Integer age;
    @JsonProperty("skills")
    private List < Object > skills = null;

    @JsonProperty("name")
    public String getName() {
        return name;
    }

    @JsonProperty("name")
    public void setName(String name) {
        this.name = name;
    }

    @JsonProperty("age")
    public Integer getAge() {
        return age;
    }

    @JsonProperty("age")
    public void setAge(Integer age) {
        this.age = age;
    }

    @JsonProperty("skills")
    public List < Object > getSkills() {
        return skills;
    }

    @JsonProperty("skills")
    public void setSkills(List < Object > skills) {
        this.skills = skills;
    }

}
🌐
Couchbase
docs.couchbase.com › sdk-api › couchbase-java-client-2.0.0 › com › couchbase › client › java › document › json › JsonArray.html
JsonArray (java-client 2.0.0 API)
Represents a JSON array that can be stored and loaded from Couchbase Server. If boxed return values are unboxed, the calling code needs to make sure to handle potential NullPointerExceptions. The JsonArray is backed by a List and is intended to work similar to it API wise, but to only allow to store such objects which can be represented by JSON. ... Creates a empty JsonArray.
Find elsewhere
🌐
Stleary
stleary.github.io › JSON-java › org › json › JSONArray.html
JSONArray
It returns an empty string if there is no value at that index. If the value is not a string and is not null, then it is converted to a string. ... A String value. public String optString(int index, String defaultValue) Get the optional string associated with an index.
🌐
Oracle
docs.oracle.com › javaee › 7 › api › javax › json › JsonArrayBuilder.html
JsonArrayBuilder (Java(TM) EE 7 Specification APIs)
A builder for creating JsonArray models from scratch. This interface initializes an empty JSON array model and provides methods to add values to the array model and to return the resulting array.
🌐
TutorialsPoint
tutorialspoint.com › how-can-we-check-if-a-json-object-is-empty-or-not-in-java
How can we check if a JSON object is empty or not in Java?\\n
JSON object is empty. Another way is the keys() method to check if a JSON object is empty or not. It returns an iterator of the keys in the JSON object. If it returns an empty iterator, then the JSON object is empty.
🌐
Stack Overflow
stackoverflow.com › questions › 60987456 › java-set-in-object-giving-empty-json-array
spring - Java Set in Object giving empty JSON array - Stack Overflow
April 2, 2020 - Following is the solution that I followed in order to get around this problem. Find the original solution here · Copyimport java.util.stream.Collectors; import javax.persistence.EntityManager; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.data.rest.core.config.RepositoryRestConfiguration; import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter; @Configuration public class MyRepositoryRestConfigurerAdapter extends RepositoryRestConfigurerAdapter { @Autowired private EntityManager entityManager; @Override public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) { config.exposeIdsFor(entityManager.getMetamodel().getEntities().stream().map(e -> e.getJavaType()).collect(Collectors.toList()).toArray(new Class[0])); } }
🌐
Stack Overflow
stackoverflow.com › questions › 12112744 › jsonarray-is-empty
java - JSONArray is empty - Stack Overflow
August 24, 2012 - */ JSONArray jArray = null; String result = null; InputStream is = null; StringBuilder sb=null; String ct_id; String ct_name; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); //http post try{ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost= new HttpPost("http://xxx.php"); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.ge