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);

    }
11
  • @Sotirios Delimanolis I changed the code to List<Result> placelist = gson.fromJson("http://localhost:3000/result",Result.class); However, it still does not change the error. Commented Nov 13, 2019 at 14:04
  • What does the Result.class at the end there represent? Commented Nov 13, 2019 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. Commented Nov 13, 2019 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<List<Result>>() {} (ie, a type representing List<Result>, a list of results). Commented Nov 13, 2019 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 Commented Nov 13, 2019 at 14:28

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.