gihan-maduranga
gihan-maduranga

Reputation: 4801

Gson malformedJsonException when casting Json string to object

I have created a Json string using Gson which is valid.I used following way to create the Json object.

Map myMap=new HashMap();
myMap.put("dryRunTest",dryRunTestObj);
myMap.put("port",22);
myMap.put("Repository",RepositoryObj);
etc...

Map temp = new HashMap();
temp.put("configuration",myMap);
new GsonBuilder().setPrettyPrinting().create().toJson(temp);
//This gave me the below Json Object that would write to a file.

{
  "configuration": {
    "dryRunTest": {
      "id": null,
      "defaultName": "sample.xml",
      "ParamsMap": null,
      "defaultVersion": "1.2",
      "dryRunTestOn": true
    },
    "port": 2323,
    "ip": "11.11.11.111",
    "configFile": "config.json",
    "Name": "007",
    "URL": "http://11.111.111.51/ma/api/ag/",
    "Password": "123",
    "configDirectory": "sss",
    "platform": "Windows7",
    "Repository": {
      "repositoryURL": "http://11.11.111.11/"
    }
  }
}

I have tried to create Repository object using Repository Json string like following.

class Repository{
String repositoryURL;
getter and setters
}

Then i desalinize serialize Json object using,

 Map mymap= new Gson().fromJson(JsonObjectReadFromFile(aboveString), HashMap.class);

RepositoryJsonStringvalue = mymap.get("Repository");

Repository r=new Gson().fromJson(RepositoryJsonStringvalue, Repository.class);

It return following exception.

com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 21

Seems like problem was with it value repositoryURL but i put same value into another top level attribute URL=http://11.11.111.11/ and then can get values but when it put inside object type it throws that error.Please let me know how to fix this.

Note:I found out problem was in repositoryURL attribute if i remove special characters : \ from string i can cast to the Repository object.So i would like to know how can i put URLhttp://11.11.111.11/ in to json object even my above object is valid Json.

Upvotes: 0

Views: 3282

Answers (2)

vkj
vkj

Reputation: 148

You can try converting the string to json string before converting it to object via gson. This happens in case of a map where you get an object if you do map.get(key).

The object wont have quotes "" around key or value.

While converting to json before , will ensure that.

RepositoryJsonStringvalue = mymap.get("Repository");
String jsonString = new Gson().toJson(RepositoryJsonStringvalue).toString();
Repository r=new Gson().fromJson(jsonString, Repository.class);

Hope it helps

Upvotes: 0

notionquest
notionquest

Reputation: 39186

The POJO class should be as follows:-

I have not included all the fields in the below POJO classes. You can include and try.

ParentConfiguration class:-

public class ParentConfiguration implements Serializable{


    private static final long serialVersionUID = -1185592608319193198L;

    private Configuration configuration;

    @Override
    public String toString() {
        return "ParentConfiguration [configuration=" + configuration + "]";
    }


}

Configuration class:-

public class Configuration implements Serializable {

    @Override
    public String toString() {
        return "Configuration [port=" + port + ", URL=" + URL + ", repository=" + repository + "]";
    }

    private static final long serialVersionUID = 8642243843212797330L;

    private Integer port;

    private String URL;



    @SerializedName("Repository")
    private Repository repository;

}

Repository class:-

public class Repository implements Serializable{

    @Override
    public String toString() {
        return "Repository [repositoryURL=" + repositoryURL + "]";
    }
    private static final long serialVersionUID = -1891563116357617243L;
    private String repositoryURL;

}

Main method:-

public static void main(String[] args) {
        String jsonString = "{  \"configuration\": {\"dryRunTest\": {\"id\": null,\"defaultName\": \"sample.xml\",      \"ParamsMap\": null,      \"defaultVersion\": \"1.2\",      \"dryRunTestOn\": true    },    \"port\": 2323,    \"ip\": \"11.11.11.111\",    \"configFile\": \"config.json\",    \"Name\": \"007\",    \"URL\": \"http://11.111.111.51/ma/api/ag/\",    \"Password\": \"123\",    \"configDirectory\": \"sss\",    \"platform\": \"Windows7\",    \"Repository\": {      \"repositoryURL\": \"http://11.11.111.11/\"    }  }}";


        Gson gson = new Gson();
        ParentConfiguration parentConfiguration = gson.fromJson(jsonString, ParentConfiguration.class);
        System.out.println(parentConfiguration.toString());
    }

Output:-

ParentConfiguration [configuration=Configuration [port=2323, URL=http://11.111.111.51/ma/api/ag/, repository=Repository [repositoryURL=http://11.11.111.11/]]]

Instead of converting to Map if you can parse it as JsonObject, you can easily achieve the result:-

import org.apache.commons.io.IOUtils;

JsonObject jsonObject= new Gson().fromJson(IOUtils.toString(ParseUrl.class.getResourceAsStream("/configuration.json")), JsonObject.class);
        Repository respository = gson.fromJson(jsonObject.getAsJsonObject("configuration").get("Repository"), Repository.class);
        System.out.println(respository);

Upvotes: 1

Related Questions