JAVA and Javascript

Asked

Viewed 77 times

3

Imagine that we are receiving a JSON via REST, so for those who use spring or jersey, usually use the Jackson that deserializes the json string for java object automatically, that at first I was impressed, but the short time I have been working with Nodejs, well the goal is not to compare, but rather to understand why in java can not be equal (which would be very good).

I learned that I need to create a DTO or VO (POJO), which represents the received json so that Jackson can deserialize and provide me with a ready-to-use JAVA object, but keep creating DTO after seeing the naturalness that this occurs with Nodejs, Poxa has made me very angry... because sometimes we receive "complex" json with various objects inside objects, inside arrays, etc...

The question is simple: Is there any way to receive a json and access its attributes without having to create Dtos?

Example:

{
   "id": "1",
   "nome": "TESTE",
   "atividades": [
       {
           "id": "1",
           "tarefa": "Fazer tal coisa"
       }
   ]
}

Now I would like to access the attributes, in Node I would do something like this, detail without Dtos:

objeto.id
objeto.nome

Already in JAVA I would have to create a DTO for the parent object and another for the array, so that Jackson deserialize, this is a bag with all respect... a platform like JAVA surely must have something better, I believe that the problem here is myself that I do not know a way, all help is welcome.

1 answer

1

There is a significant difference between the framework Node.js and Java which is the fact that Java is a strongly typed language while Node.js (Javascript) does not. The way objects are created and manipulated are different.

Javascript for example can consume a service REST and parse the result for a javascript object, providing the auto-use for. in to traverse the keys and values, or Array#foreach in case of arrays - this really is wonderful.

The question is simple: Is there any way to receive a json and access its attributes without having to create Dtos?

As an alternative to your doubt, you can use the Gson from Google to deserialize a JSON for an Object Java and then access the keys and values, but even so, if there are nested objects and/or arrays (which are also objects) the treatment will not be so generic, you will have to inform what to do with them if you want to access a sub-tribute.

Example:

public static void main(String[] args){

    String json = "{\"id\": \"1\",\"nome\": \"TESTE\",\"atividades\": [{\"id\": \"1\",\"tarefa\": \"Fazer tal coisa\"}]}";
    Map<String, Object> a = new Gson().fromJson(json, Object.class);
    for (Map.Entry<String, Object> entry : a.entrySet()) {
        String key = entry.getKey();
        Object value = entry.getValue();
        System.out.println(key + " => " + value);
    }

}

Exit:

id => 1
nome => TESTE
atividades => [{id=1, tarefa=Fazer tal coisa}]
  • Hello Lucas, Great explanation, thank you very much!! I think that for these and other reasons that Nodejs has been gaining a lot of space quickly, I notice a lot of naturalness in programming JS, of course it should have its negative points, but for now I don’t see many reasons to use JAVA in the day-day, multi-platform is no longer such a big advantage. I’m a fan of C++ and JAVA maybe someone brings some idea we don’t know yet. !!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.