GSON - JsonSyntaxException - Expected name at line 7 column 4

50,013

Solution 1

If this is the actual json: You have an extra comma here and a spelling error. The error says you have bad json syntax. So this is probably one of the first places to look.

{
            "objectid" : "test",
            "dtype" : "test",
            "type" : "test",
            "name " : "test",
            "description" : "test", //delete this comma
            },
            {
            "objectid" : "test",
            "dtyoe" : "test",  // spelling error
            "type" : "test",
            "name " : "test",
            "description" : "test"
    }

You also seem to be parsing two objects and telling gson you want one result object from it. Consider either parsing the objects separately or tell gson you want a result array Back

Solution 2

use

catch(JsonSyntaxException e)

instead of

catch(MalformedJsonException e)

because MalformedJsonException is some internal exception while JsonSyntaxException is the one that actually get thrown. here is a code snippet

            String response="Something";
            JsonElement my_json;
            try {
                my_json=jsonParser.parse(response);
            } catch(JsonSyntaxException e) {
                e.printStackTrace();
                JsonReader reader = new JsonReader(new StringReader(response));
                reader.setLenient(true);
                my_json=jsonParser.parse(reader);
            }

Solution 3

You have two objects but getting one object from GSON. either you should use below format of JSON string to get Result object from this JSON string.

{
    "objectid" : "test",
    "dtype" : "test",
    "type" : "test",
    "name" : "test",
    "description" : "test" 

}

Second option :-

You will have to change JSON string format. you will have to change it in JSON array and get ArrayList of this object, After that, we can get Result object using array list index.new JSON string would be like this.

    {"resultArray":[{
    "objectid" : "test",
    "dtype" : "test",
    "type" : "test",
    "name" : "test",
    "description" : "test" 
           },
      {
    "objectid" : "test",
    "dtype" : "test",
    "type" : "test",
    "name" : "test",
    "description" : "test"
        }]}

And java code to fetch Result Object.

Gson g = new Gson();
ResultList r = g.fromJson(res.toString(), ResultList.class);
ArrayList<Result> resultList = r.getResultList();
       for(int x=0 ;x<resultList.size();x++){
              Result result = resultList.get(x);
              }

ResultList Model is:-

public class ResultList {
@SerializedName("resultArray")
public ArrayList<Result> resultList;
public getResultList(){
          return resultList;
           } 
       }

@Note: - Result.java would be used same as given above.

Share:
50,013
Namenoobie
Author by

Namenoobie

Updated on August 17, 2022

Comments

  • Namenoobie
    Namenoobie over 1 year

    I have the following result class whose object is to be returned as JSON.

    public class Result {
        public String objectid;
        public String dtype;
        public String type;
        public String name;
        public String description;
    
        public Result() {
            this.objectid = "";
            this.dtype= "";
            this.type="";
            this.name= "";
            this.description = "";
        }
    
        public String getObjectid() {
            return objectid;
        }
    
        public void setObjectid(String s) {
            this.objectid = s;
        }
    
        public String getDtype() {
            return dtype;
        }
    
        public void setDtype(String s) {
            this.dtype= s;
        }
    
        public String getType() {
            return type;
        }
    
        public void setType(String s) {
            this.type = s;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String s) {
            this.name = s;
        }
    
        public String getDescription() {
            return description;
        }
    
        public void setDescription(String s) {
            this.description = s;
        }
    
    }
    

    I have a configuration json which is read my the main .java and returned as json as HTTP RESPONSE. It is as below:

    {
            "objectid" : "test",
            "dtype" : "test",
            "type" : "test",
            "name" : "test",
            "description" : "test" // removed
    },
    {
            "objectid" : "test",
            "dtype" : "test",
            "type" : "test",
            "name" : "test",
            "description" : "test"
    }
    

    Main .java

    Using Gson, it reads the configuration.json file and has to return a json.

    My code:

    Gson g = new Gson();
    Result r = g.fromJson(res.toString(), Result.class);
    

    where res.toString() gets me the configuration.json file content as string.

    My problem:

    I am experiencing the following exception:

    Exception com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 7 column 3
    com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 7 column 3

    Any pointers?

    • CBIII
      CBIII almost 11 years
      why is there a spaced in "name "? Your error Says you have bad JSON
    • Namenoobie
      Namenoobie almost 11 years
      @ClydeByrdIII i fixed it, sorry. still the exact issue.
    • Dan W
      Dan W almost 11 years
      Are you trying to parse multiple Results from that Json? If so, you need your Json to look like a list and tell Gson that it's a list too.
    • Namenoobie
      Namenoobie almost 11 years
      @DanW No, just one Result.
    • CBIII
      CBIII almost 11 years
      If it's one result you shouldn't be parsing two objects at the same time
    • Namenoobie
      Namenoobie almost 11 years
      @ClydeByrdIII thank you for your reply. Then could I pass a JSONArray ? Meaning in the configuration.json, shall I create a jsonarray with the two objects? How would that reflect on my main .java file?
    • CBIII
      CBIII almost 11 years
      You can do that or tell gson you want a result array.
    • Namenoobie
      Namenoobie almost 11 years
      @ClydeByrdIII could you elaborate?
    • CBIII
      CBIII almost 11 years
    • Namenoobie
      Namenoobie almost 11 years
      I have changed my configuration.json as [ {object1}, {object2} ]. With the same main .java code, I get Exception com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2
    • CBIII
      CBIII almost 11 years
      are you even trying to interpret your errors? You need to read your config file, parse the string into a JSON array object and then deserialize each object in to individual result objects.
    • CBIII
      CBIII almost 11 years
      You also have a spelling error in the second object
    • Namenoobie
      Namenoobie almost 11 years
      I have changed the spelling, thanks.
  • Namenoobie
    Namenoobie almost 11 years
    I don't understand. I don't have any extra opening curly brace there.
  • Namenoobie
    Namenoobie almost 11 years
    So there are two objects, comma seperated, inside configuration.json
  • CBIII
    CBIII almost 11 years
    sorry I thought it was objects in objects, I changed my answer. To that trailing comma you have
  • Namenoobie
    Namenoobie almost 11 years
    Exception com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 7 column 3 com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 7 column 3
  • Tinashe Chinyanga
    Tinashe Chinyanga about 2 years
    Helped resolve my issue.