-1

I'm a beginner in programming, and I need some help. Is it possible to convert an HTTP (that returns a Json) automatic call to object in java? For example it reads the request, and when I call System.out.println (obj) it already returns me an OBJECT of this request, instead of String. Is it possible? If so, could you help me ... I already did the method to call the url and return string, but I need to return OBJECT, so I can compare with HashCode and Equals.

My code:

enter image description here output: {"header":{"messageId":"02938ec7-b2c3-4131-8ecf-3ad3a8509b41"},"body":{"products"

What I wanted: output Informacoes [header=Header [messageId=66d22c00-bddc-4ea7-afbd-7c7225fcb914], body=Body

Lamick
  • 1
  • 3

1 Answers1

0

From what I can understand from your question, it looks like Gson might be useful. Gson is a library that allows you to convert between JSON and Java primitives/objects. Here's an example I just wrote:

    class BagOfPrimitives {
        private int value1 = 1;
        private String value2 = "abc";
    }

    public static void main(String[] args) {
        Gson gson = new Gson();
        String json = "{\"value1\":1,\"value2\":\"abc\"}";
        BagOfPrimitives obj = gson.fromJson(json, BagOfPrimitives.class);
    }

This code converts the json {"value1":1,"value2":"abc"} into an object of the class BagOfPrimitives.

To add Gson to your project, go here, click "Downloads" at the top right, and click "jar". Then follow these instructions to add the jar file to your project. Then you should be able to write import com.google.gson.* at the top of your class and use Gson in your java code.