String to Hash map to Gson conversion to give error com.google.gson.stream.MalformedJsonException:Unterminated object

时间:2016-04-21 12:48:31

标签: java json gson

下面是给出错误的函数

public Document<?> post(String v1, String app, CacheType objectType, String bucketName, String password,
        Document<?> customDoc)
                throws IOException, ClassNotFoundException, InternalException {
    String uri = null;
        uri = getURI() + "/" + v1 + "/postDoc/" + app + "/" + objectType.toString() + "/" + bucketName;
    String jsonRequest = populateRequest(objectType.toString(), customDoc);
    String jsonResponse = sendRestCallPost(uri, jsonRequest, password);

JsonResponse包含{"code":500,"message":"com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 39 path $.firstKey.content"}

以下是populateRequest函数

public static String populateRequest(String objectType, Document<?> response) {
    String _CONTENT = "content";
    String _EXPIRY = "expiry";
    HashMap<String, Object> jsonObj = new HashMap<>();
    HashMap<String, Object> map = new HashMap<>();
    if(objectType.equals("STRING")){
        jsonObj = (HashMap<String, Object>) populateToString(response);
    }
    else{
        map.put(_CONTENT, response.content());
        map.put(_EXPIRY, response.expiry());
        jsonObj.put(response.id(), map);
    }
    String gson = jsonObj.toString();
    return gson;
}

public static HashMap<?,?> populateToString(Document<?> response){

    HashMap<String, Object> jsonObj = new HashMap<>();
    HashMap<String, Object> map = new HashMap<>();
    String _CONTENT = "content";
    String _EXPIRY = "expiry";
    String id = response.id().toString().trim();
    String content = (String) response.content().toString().trim();
    map.put(_CONTENT, content);
    map.put(_EXPIRY, response.expiry());
    jsonObj.put(id, map);
    return jsonObj;
}

,当调用populateRequest函数时,变量响应包含JsonStringDocument{id='firstKey', cas=0, expiry=4000, content=json doc with first key}

编辑这是测试我的代码的Junit

public void testRemoveDoc() throws Exception {
    String id = "firstKey";
    couchCacheBucket = new CouchCacheBucket(instance, bucketName, password, applicationName, retry, host, port,
            useRest);
    // Data Preparation Step - Getting a document
    JsonStringDocument str = null;
    try {
        str = (JsonStringDocument) couchCacheBucket.getDoc(id, CacheType.STRING_TYPE);

        if (str==null) {
            String body = "json doc with first key";
            JsonStringDocument jsonStr = JsonStringDocument.create(id, 4000, body);
            Document doc = couchCacheBucket.insertDoc(jsonStr, CacheType.STRING_TYPE);

JsonObj 包含{firstKey={expiry=4000, content=json doc with first key}}

Gson 包含{firstKey={expiry=4000, content=json doc with first key}}

JsonRequest 包含{firstKey={expiry=4000, content=json doc with first key}}

2 个答案:

答案 0 :(得分:2)

我看到你的错误堆栈

 MalformedJsonException: Unterminated object at line 1 column 39 path $.firstKey.content

这意味着您作为参数发送到String的{​​{1}}对象存在问题。

您要做的是将REST-Api对象直接更改为HashMap 所以,在你的代码中

方法

String

使用public static String populateRequest(String objectType, Document<?> response)返回生成的String对象。

在每个HashMapkey中不包含双引号 value,而不是"key":"value"对象包含String

因此,在将key:value参数解析为String业务逻辑代码JSONObject时,这可能会引发异常。

例如,如果您尝试此代码

REST-API

会打印

HashMap<String, String> map = new HashMap<>();
map.put("first", "value1");
map.put("second", "value1" );
System.out.println(map.toString());

不像这样。

{
first=value1,  //don't have "".
second=value2,
}
  

解决方案: - 请将{ "first" : "value1", "second" = "value2" //contains "". } 添加到您的图书馆 Jar Download Link

并尝试使用此代码将gson-2.2.2.jar更改为HashMap

Json

答案 1 :(得分:0)

请将gson-2.2.2.jar添加到您的图书馆 Jar Download Link

并尝试使用此代码将HashMap更改为Json

HashMap<String, String> complexHashMapObject = new HashMap<>();
Gson gsone = new Gson();
JsonObject res = gsone.toJsonTree(complexHashMapObject).getAsJsonObject();