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);
}
>() {}` (ie, a type representing `List`, a list of results).
– Sotirios Delimanolis Nov 13 '19 at 14:11