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

Asked

Viewed 1,117 times

1

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

Displays the error com.google.gson.stream.MalformedJsonException. I have already checked several times and even using websites that check the syntax of JSON and the syntax seems to be correct.

I developed a minimal and executable example, replacing the whole part of HTTP requests with a string that stores JSON to simplify since the other parts do not present errors and have data that I cannot pass.

My question is how to solve this mistake com.google.gson.stream.MalformedJsonException 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

  • Is this string ai your json file? Lacked the class q converts json.

  • Yes, it’s json. I put the method that converts json to list in Json.class. Its name is Jsontolist

1 answer

2


The problem is these quotes from your json, you didn’t notice that the java didn’t even ask you to escape them? Because they are not valid quotes, so json cannot format. I switched them all with double quotes(") 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" +
    "}" +
    "]";

<inserir a descrição da imagem aqui

Remembering you need to escape with \.

  • Something so simple that gave me the biggest beating. And look that on two sites that Validei the json he passed without problems. Thank you very much for the help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.