Error converting JSON. (com.google.gson.stream.MalformedJsonException)

1

I'm developing a program that needs to communicate with an online store, all communication is via HTTP requests and has a JSON response.

Displays the com.google.gson.stream.MalformedJsonException error. I have checked it several times and even using sites that give the JSON syntax and the syntax seems to be correct.

I developed a minimal and executable example, replacing all of the HTTP requests with a string that stores JSON for simplicity since the other parts do not contain errors and have data that I can not pass.

My question is how to resolve this com.google.gson.stream.MalformedJsonException error in my program. (Follows error log)

Json.class

import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.List;
import javax.swing.JOptionPane;

public class Json {

    public static void main(String[] args) {
        JOptionPane.showMessageDialog(null, "O resultado é exibido no console.");


                String strJson = "[\n" +
                "{\n" +
                "“id”: 1,\n" +
                "“createdAt”: “2016-12-27T10:58:13-02:00”,\n" +
                "“updatedAt”: “2016-12-27T14:57:39-02:00”,\n" +
                "“erpId”: “Armazém 1”,\n" +
                "“name”: “Armazém Revenda”,\n" +
                "“priority”: 1,\n" +
                "“branch”: {\n" +
                "“id”: 1,\n" +
                "“erpId”: “Filial 1”,\n" +
                "“name”: “Filial 1”,\n" +
                "“documentId”: “”,\n" +
                "“createdAt”: “2016-12-27T10:58:13-02:00”,\n" +
                "“updatedAt”: “2016-12-27T10:58:13-02:00”\n" +
                "},\n" +
                "“default”: true,\n" +
                "“quantity”: 1\n" +
                "}\n" +
                "]";

        List<Warehouses> lista = JSONtoList(strJson);

        for(Warehouses w: lista) {
            System.out.println(w.getId());
            System.out.println(w.isDef());
        }

    }

    public static List<Warehouses> JSONtoList(String strJson) {
        Type type = new TypeToken<List<Warehouses>>() {
        }.getType();

        Gson gson = new GsonBuilder().setDateFormat("dd/MM/yyyy").create();
        List<Warehouses> lista = gson.fromJson(strJson, type);

        return lista;
    }

}

Branch.class

import java.util.Date;

public class Branch {

    private int id;
    private String erpId;
    private String name;
    private String documentId;
    private Date createdAt;
    private Date updateAt;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getErpId() {
        return erpId;
    }
    public void setErpId(String erpId) {
        this.erpId = erpId;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDocumentId() {
        return documentId;
    }
    public void setDocumentId(String documentId) {
        this.documentId = documentId;
    }
    public Date getCreatedAt() {
        return createdAt;
    }
    public void setCreatedAt(Date createdAt) {
        this.createdAt = createdAt;
    }
    public Date getUpdateAt() {
        return updateAt;
    }
    public void setUpdateAt(Date updateAt) {
        this.updateAt = updateAt;
    }

}

Warehouses.class

import java.util.Date;

import com.google.gson.annotations.SerializedName;

public class Warehouses {

    private int id;
    private Date createdAt;
    private Date updateAt;
    private String erpId;
    private String name;
    private int priority;
    private Branch branch;
    @SerializedName("default")
    private boolean def;
    private int quantity;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public Date getCreatedAt() {
        return createdAt;
    }
    public void setCreatedAt(Date createdAt) {
        this.createdAt = createdAt;
    }
    public Date getUpdateAt() {
        return updateAt;
    }
    public void setUpdateAt(Date updateAt) {
        this.updateAt = updateAt;
    }
    public String getErpId() {
        return erpId;
    }
    public void setErpId(String erpId) {
        this.erpId = erpId;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getPriority() {
        return priority;
    }
    public void setPriority(int priority) {
        this.priority = priority;
    }
    public Branch getBranch() {
        return branch;
    }
    public void setBranch(Branch branch) {
        this.branch = branch;
    }
    public boolean isDef() {
        return def;
    }
    public void setDef(boolean def) {
        this.def = def;
    }
    public int getQuantity() {
        return quantity;
    }
    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

}

Error

Exception in thread "main" com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated object at line 4 column 29 path $[0].null
    at com.google.gson.Gson.fromJson(Gson.java:942)
    at com.google.gson.Gson.fromJson(Gson.java:892)
    at com.google.gson.Gson.fromJson(Gson.java:841)
    at json.Json.JSONtoList(Json.java:56)
    at json.Json.main(Json.java:42)
Caused by: com.google.gson.stream.MalformedJsonException: Unterminated object at line 4 column 29 path $[0].null
    at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1568)
    at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:491)
    at com.google.gson.stream.JsonReader.hasNext(JsonReader.java:414)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:216)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
    at com.google.gson.Gson.fromJson(Gson.java:927)
    ... 4 more

Add to Lib Gson! Download

    
asked by anonymous 24.11.2018 / 15:14

1 answer

2

The problem is these quotes from your json, did not you notice that java did not even ask you to escape them? Because they are not valid quotation marks, so json can not format. I replaced them all with double quotation marks (") and the code ran smoothly.

    String strJson = "[" +
    "{" +
    "\"id\": 1," +
    "\"createdAt\": \"2016-12-27T10:58:13-02:00\"," +
    "\"updatedAt\": \"2016-12-27T14:57:39-02:00\"," +
    "\"erpId\": \"Armazém 1\"," +
    "\"name\": \"Armazém Revenda\"," +
    "\"priority\": 1," +
    "\"branch\": {" +
    "\"id\": 1," +
    "\"erpId\": \"Filial 1\"," +
    "\"name\": \"Filial 1\"," +
    "\"documentId\": \"\"," +
    "\"createdAt\": \"2016-12-27T10:58:13-02:00\"," +
    "\"updatedAt\": \"2016-12-27T10:58:13-02:00\"" +
    "}," +
    "\"default\": true," +
    "\"quantity\": 1" +
    "}" +
    "]";

<

Remembering that you must escape with \ .

    
24.11.2018 / 15:43