0

I'm trying to capture and print the contents of a JSON file using Java. I first set up my localhost on port 3000 to be the place where my GSON will look for the JSON file. However, when I go to implement the code in Java I keep getting this error. Expected BEGIN_OBJECT but was STRING at line 1 column 5. Now I have been researching on Stack Overflow and found out it is because i am basically passing back an invalid return type. Though once I made the needed adjustments, it still doesn't seem to work properly and keeps passing back that error. I have used these link to guide me "Expected BEGIN_OBJECT but was STRING at line 1 column 1"

Localhost JSON:

[
  {
    "u_acronym": "AdHoc",
    "name": "Ad Hoc Reporting (M&R) (AdHoc)",
    "u_application_entry_code": "PZ6"
  }
]

Code:


import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Result {

    @SerializedName("u_acronym")
    @Expose
    private String uAcronym;
    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("u_application_entry_code")
    @Expose
    private String uApplicationEntryCode;

    public Result(String uAcronym, String name, String uApplicationEntryCode, String status) {
        this.uAcronym = uAcronym;
        this.name = name;
        this.uApplicationEntryCode = uApplicationEntryCode;
    }

    public String getUAcronym() {
        return uAcronym;
    }

    public void setUAcronym(String uAcronym) {
        this.uAcronym = uAcronym;
    }

    public Result withUAcronym(String uAcronym) {
        this.uAcronym = uAcronym;
        return this;
    }

    public String getName() {
        return name;
    }

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

    public Result withName(String name) {
        this.name = name;
        return this;
    }

    public String getUApplicationEntryCode() {
        return uApplicationEntryCode;
    }

    public void setUApplicationEntryCode(String uApplicationEntryCode) {
        this.uApplicationEntryCode = uApplicationEntryCode;
    }

    public Result withUApplicationEntryCode(String uApplicationEntryCode) {
        this.uApplicationEntryCode = uApplicationEntryCode;
        return this;
    }

}

RestService Call:**

public static void callRestService()  {
        Gson gson = new Gson();

        Result placelist = gson.fromJson("http://localhost:3000/result", Result.class); 

        System.out.println(placelist);

    }
kane_004
  • 193
  • 1
  • 10
  • @Sotirios Delimanolis I changed the code to ```List placelist = gson.fromJson("http://localhost:3000/result",Result.class); ``` However, it still does not change the error. – kane_004 Nov 13 '19 at 14:04
  • What does the `Result.class` at the end there represent? – Sotirios Delimanolis Nov 13 '19 at 14:06
  • @SotiriosDelimanolis It should represent the the object inside the array. So the u_application_app_code/ name /u_acro. My end goal is to retrieve the contents of that JSON. – kane_004 Nov 13 '19 at 14:09
  • No, the `Result.class` tells Gson to interpret the JSON as a `Result`. As the duplicate states, Gson can't interpret a JSON array as a POJO. You'll want that last argument to be a `Result[].class` (ie, an array of `Result` objects) or a `new TypeToken>() {}` (ie, a type representing `List`, a list of results). – Sotirios Delimanolis Nov 13 '19 at 14:11
  • @SotiriosDelimanolis Yeah, so even when I try it that way, I still get the same error. ```gson.fromJson("http://localhost:3000/result",Result[].class); ```. I still receive that ```Expected BEGIN_OBJECT but was STRING at line 1 column 5 ``` – kane_004 Nov 13 '19 at 14:28
  • Oh, the first argument you pass to `fromJson` is mean to be the JSON. It's not interpreted as a URL that needs to be queried and its response extracted. Do the HTTP request yourself, extract the body and pass that to `fromJson`. – Sotirios Delimanolis Nov 13 '19 at 14:30
  • @SotiriosDelimanolis wow ok, now I understand and get the error. Haha the whole goal, was for me to do the URL call. Do you know of any way or Java class using Spring boot that I can make a URL call to get the JSON? – kane_004 Nov 13 '19 at 14:35
  • https://stackoverflow.com/questions/1359689/how-to-send-http-request-in-java – Sotirios Delimanolis Nov 13 '19 at 14:36
  • @SotiriosDelimanolis Thank you, I will give it a look – kane_004 Nov 13 '19 at 14:46
  • @SotiriosDelimanolis Turns out I had to use the RestTemplate class in Spring Boot to call the URL. Then, I used the line of code mentioned above to parse it. Thanks again – kane_004 Nov 13 '19 at 16:41
  • Note that `RestTemplate` has integration with Gson through `GsonHttpMessageConverter`. So you don't have to extract the body as a `String` and pass it to `fromJson`. You can straight up use `RestTemplate`'s `exchange` method with a `ParameterizedTypeReference` similar to the `TypeToken`. – Sotirios Delimanolis Nov 13 '19 at 16:48

0 Answers0