Java 字符串到哈希映射到Gson转换,给出错误com.google.Gson.stream.MalformedJsonException:未终止的对象
下面是给出错误的函数Java 字符串到哈希映射到Gson转换,给出错误com.google.Gson.stream.MalformedJsonException:未终止的对象,java,json,gson,Java,Json,Gson,下面是给出错误的函数 public Document<?> post(String v1, String app, CacheType objectType, String bucketName, String password, Document<?> customDoc) throws IOException, ClassNotFoundException, InternalException { String
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);
调用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}}
我看到了您的错误堆栈
MalformedJsonException: Unterminated object at line 1 column 39 path $.firstKey.content
这意味着作为参数发送到restapi
的String
对象有问题
您试图做的是将HashMap
对象直接更改为String
那么,在你的代码中
方法
公共静态字符串populateRequest(字符串对象类型,文档响应)
返回使用HashMap
生成的String
对象
在每个键和值中不包含双引号“键”:“值”
而不是此字符串对象包含键:值
因此,在REST-API
业务逻辑代码中将String
参数解析为JSONObject
时,这可能会引发异常
例如,如果您尝试此代码
HashMap<String, String> map = new HashMap<>();
map.put("first", "value1");
map.put("second", "value1" );
System.out.println(map.toString());
不像这样
{
"first" : "value1",
"second" = "value2" //contains "".
}
解决方案:-请将gson-2.2.2.jar
添加到您的库中
并尝试使用此代码将您的HashMap
更改为Json
HashMap<String, String> complexHashMapObject = new HashMap<>();
Gson gsone = new Gson();
JsonObject res = gsone.toJsonTree(complexHashMapObject).getAsJsonObject();
HashMap<String, String> complexHashMapObject = new HashMap<>();
Gson gsone = new Gson();
JsonObject res = gsone.toJsonTree(complexHashMapObject).getAsJsonObject();
hashMapcomplexhashMapObject=newhashMap();
Gson gsone=新的Gson();
JsonObject res=gsone.toJsonTree(complexhashmappobject.getAsJsonObject();
请将gson-2.2.2.jar
添加到您的库中
并尝试使用此代码将您的HashMap
更改为Json
HashMap<String, String> complexHashMapObject = new HashMap<>();
Gson gsone = new Gson();
JsonObject res = gsone.toJsonTree(complexHashMapObject).getAsJsonObject();
HashMap<String, String> complexHashMapObject = new HashMap<>();
Gson gsone = new Gson();
JsonObject res = gsone.toJsonTree(complexHashMapObject).getAsJsonObject();
hashMapcomplexhashMapObject=newhashMap();
Gson gsone=新的Gson();
JsonObject res=gsone.toJsonTree(complexhashmappobject.getAsJsonObject();
你能用第一个键发布content=json文档吗?我感觉你在错误地嵌套JSON。或者内容JSON本身无效。我没有确切地告诉你我要发布什么?您能解释一下吗?在您的示例中,“带第一个键的json文档”是什么?它是一根绳子吗?JSON?它的字面意思是“带第一个键的json文档”吗?如果是这样,它不应该被引用吗?它是一个字符串。我已经编辑了我的帖子来展示我是如何使用它的。你能发布从populateRequest()
返回的字符串jsonRequest吗?因为它看起来像是由sendRestCallPost()
生成的错误。这似乎是接收jsonRequest的服务器抛出的500服务器错误。是的,这正是问题所在btu如何处理它?String gson=jsonObj.toJson()
而不是字符串gson=jsonObj.toString()
ru使用任何JSON
库?是的,我使用的是Gson和Jsonanswer。你应该试试这个可能对你有用@Legendary_Hunter