我实际上是这样将我的pojo数据转换成json字符串的,
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
String json=gson.toJson(user);
我得到了 json 字符串,但这不是我实际需要的格式,我得到了
json = {"userID":300,"userName":"asd","password":"s","enabled":1}
所以,我想将 Json 字符串转换为键值对,如下所示,
{"userID":300,"userName":"asd","password":"s","enabled":1}
转换为只有值(没有键)的 Json 字符串,如下所示
[300,"asd","s",1]
最佳答案
所以我在你的字符串 json
之后继续。
// lets deserialize your json string and get a hashmap
Type collectionType = new TypeToken<HashMap<String, Object>>(){}.getType();
HashMap<String, Object> hm = gson.fromJson(json, collectionType);
String finalJson = gson.toJson(hm.values());
// aand taa-daa!!
System.out.println(finalJson);
现在 finalJson
是 [300,"asd","s",1]
编辑:库如下:
import java.lang.reflect.Type;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
关于java - 如何将带有键值对的Json字符串转换为只有值的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16541938/