0

I am creating an app that makes a GET Request to an API and receives a JSON and I need to create class that holds the information. This is what I have tried :

package apiwebapprequest;

import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;


public class APIWebAppRequest {


public static void main(String[] args) throws IOException {
    Gson gson = new Gson();
    Object obj = new Object();

    try {   
 URL url = new URL("xxx");
 URLConnection yc = url.openConnection();
 BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
 String inputLine;

 while((inputLine = in.readLine())!= null){
     System.out.println(inputLine);

     in.close();
     gson.toJson(obj,inputLine);




 }

 }catch(Exception e) {System.out.println(e);}



}

}

This is the object class:

package apiwebapprequest;

public class Object {


private String cif;
private String data_creare;

public String getCif() {
    return cif;
}

public void setCif(String cif) {
    this.cif = cif;
}

public String getData_creare() {
    return data_creare;
}

public void setData_creare(String data_creare) {
    this.data_creare = data_creare;
}




}

On line 29 - > gson.toJson(obj,inputLine); it gives me an error. Can you please tell me how to do it ? I can`t find how to fix it or modify in other way that it works

The ideea is that i get out when i make request in inputLine I have json and I want to save the fields separate in Object properties

3
  • 3
    "it gives me an error" - are we supposed to guess the error? It would be easier to help you if you'd provide the error message for us. Commented Jan 27, 2020 at 10:22
  • Other than that, there is a lot of resources online on how to read/parse JSON in Java, eg. this article Commented Jan 27, 2020 at 10:25
  • check my answer you can use fromJson(json, Car.class) Commented Jan 27, 2020 at 10:34

4 Answers 4

1

You can use the following way.

gson.toJson(obj); // is used to convert object to JSON

if you want to convert JSON to Java Object then you can use

gson.fromJson(json, Car.class);

Ex.

     public class Car {
            public String brand = null;
            public int    doors = 0;
   // add getter and setter

        }

String json = "{\"brand\":\"Jeep\", \"doors\": 3}";

Gson gson = new Gson();

Car car = gson.fromJson(json, Car.class);
Sign up to request clarification or add additional context in comments.

2 Comments

you hard-coded the values, how can i take from variable that contains json ?
@SECI If you don't know the structure of the JSON data beforehand, you can use the low-level Parser API of Gson, e.g. JsonParser.parseString(jsonString) will return a JsonElement object which you can then analyze further.
1

You should read the entire response first in the while loop and then convert to json after while loop ends. You can modify your code something like

StringBuilder sb = new StringBuilder();
while((inputLine = in.readLine())!= null){
     System.out.println(inputLine);
     sb.append(inputLine +"\n");
     in.close();
 }
gson.toJson(obj,sb.toString());

2 Comments

It says no suitable method found for toJson , do you know how to use Gson can you please help me with the correct method ? Thanks in advance
You can do SoModel model = gson.fromJson(sb.toString(), SoModel.class); where SoModel is the java model class similar to structure of json response of service
0

Try using gson.toJson(obj,inputLine) after the while loop.

Suppose the json is

{
"foo":"bar",
"foo1":"bar1"
}

Each line is not a valid JSON. Hence you need to read the entire string 1st and then parse it.

Comments

0

You can use Jackson class com.fasterxml.jackson.databind.ObjectMapper.

Following is how you can parse the json to the class:

ObjectMapper objectMapper = new ObjectMapper();
MyClass myClass = objectMapper.readValue(json, MyClass.class);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.