值應該不會有行情JSON對象我有一個JSON它看起來像這樣:構建在使用GSON
{
"key1": "value1",
"key2": "value2",
"key3": "value3",
"key4": [{
"key11": {
"key21": "value21"
}
}, {
"key12": "value12"
}]
}
,並需要得到JSON爲key4
每個值。
{
"key1": "value1",
"key2": "value2",
"key3": "value3",
"key4": {
"key11": {
"key21": "value21"
}
}
}
和
{
"key1": "value1",
"key2": "value2",
"key3": "value3",
"key4": {
"key12": "value12"
}
}
但有我的輸出如下:
{
"key1": "value1",
"key2": "value2",
"key3": "value3",
"key4": "{\"key11\": {\"key21\": \"value21\"}}"
}
我使用JSON建設者和Java對象轉換成JSON對象。
public class BuildJson {
private String key1 = "value1";
private String key2 = "value2";
private String key3 = "value3";
private String key4;
public BuildJson(String value4) {
key4 = value4;
}
public String buildJson(BuildJson bj){
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String new_json = gson.toJson(bj);
return new_json;
}
}
初始化構造函數並調用buildJson
方法。
List <String> key4_object; //holds list of values
for (String i : key4_object){
BuildJson bj = new BuildJson(i);
String new_json = bj.buildJson(bj);
System.out.print(gson.toJson(new_json));
}
由於key4
是String
型的,它是加上引號和值始終String
。
如何避免雙引號爲key4
?
但是,什麼是錯用雙引號呢? –
生成的JSON將作爲對Web請求的響應返回,並且不會正確顯示。而且key4是複雜的嵌套JSON。 – Naive
你只想返回一個複雜的JSON? –