“Expected BEGIN_OBJECT but was STRING at line 1 column 1”

前端 未结 14 2208
误落风尘
误落风尘 2020-11-22 01:11

I have this method:

public static Object parseStringToObject(String json) {
    String Object = json;
    Gson gson = new Gson();
    Object objects = gson.f         


        
相关标签:
14条回答
  • 2020-11-22 02:02

    In my case, I am Returning JSON Object as

    {"data":"","message":"Attendance Saved Successfully..!!!","status":"success"}

    Resolved by changing it as

    {"data":{},"message":"Attendance Saved Successfully..!!!","status":"success"}

    Here data is a sub JsonObject and it should starts from { not ""

    0 讨论(0)
  • 2020-11-22 02:03

    I had a similar problem recently and found an interesting solution. Basically I needed to deserialize following nested JSON String into my POJO:

    "{\"restaurant\":{\"id\":\"abc-012\",\"name\":\"good restaurant\",\"foodType\":\"American\",\"phoneNumber\":\"123-456-7890\",\"currency\":\"USD\",\"website\":\"website.com\",\"location\":{\"address\":{\"street\":\" Good Street\",\"city\":\"Good City\",\"state\":\"CA\",\"country\":\"USA\",\"postalCode\":\"12345\"},\"coordinates\":{\"latitude\":\"00.7904692\",\"longitude\":\"-000.4047208\"}},\"restaurantUser\":{\"firstName\":\"test\",\"lastName\":\"test\",\"email\":\"test@test.com\",\"title\":\"server\",\"phone\":\"0000000000\"}}}"
    

    I ended up using regex to remove the open quotes from beginning and the end of JSON and then used apache.commons unescapeJava() method to unescape it. Basically passed the unclean JSON into following method to get back a cleansed one:

    private String removeQuotesAndUnescape(String uncleanJson) {
        String noQuotes = uncleanJson.replaceAll("^\"|\"$", "");
    
        return StringEscapeUtils.unescapeJava(noQuotes);
    }
    

    then used Google GSON to parse it into my own Object:

    MyObject myObject = new.Gson().fromJson(this.removeQuotesAndUnescape(uncleanJson));
    
    0 讨论(0)
提交回复
热议问题