2
我需要創建一些不帶屬性名稱的JSON
輸出。這是發送給瀏覽器數據表(datatables.net),和數據表預計JSON
,像這樣:如何編寫不帶屬性名稱的JSON值
{
"aaData": [
[
"Trident",
"Internet Explorer 4.0",
"Win 95+",
"4",
"X"
],
[
"Trident",
"Internet Explorer 5.0",
"Win 95+",
"5",
"C"
]
]
}
(「服務器響應」樣品從here向下滾動)
因爲我的數據是不是從一個已知的類來了,我對在內存中創建一個樹寫入數據,然後調用rootNode.toString():
public String jsonTest()
{
final JsonNodeFactory nodeFactory = JsonNodeFactory.instance;
ObjectNode rootNode = nodeFactory.objectNode();
ArrayNode arrayNode = nodeFactory.arrayNode();
rootNode.put("aaData", arrayNode);
for (int i = 0; i < 3; i++) {
ObjectNode dataNode = nodeFactory.objectNode();
arrayNode.add(dataNode);
dataNode.put("field1", "Trident");
dataNode.put("field2", "Internet Explorer 4.0");
dataNode.put("field3", "Win 95+");
dataNode.put("field4", "4");
dataNode.put("field5", "X");
}
return rootNode.toString();
}
這產生:
{
"aaData": [
{
"field1": "Trident",
"field2": "Internet Explorer 4.0",
"field3": "Win 95+",
"field4": "4",
"field5": "X"
},
{
"field1": "Trident",
"field2": "Internet Explorer 4.0",
"field3": "Win 95+",
"field4": "4",
"field5": "X"
},
{
"field1": "Trident",
"field2": "Internet Explorer 4.0",
"field3": "Win 95+",
"field4": "4",
"field5": "X"
}
]
}
我的問題:我怎樣才能完全抑制「field1」,「field2」等,以便我得到json數據表所期望的(如上所示)?
你似乎遇到麻煩JSON數組告訴相隔JSON對象(你產生了什麼)(見@ IzaazYunus的答案) – fge
爲什麼要添加毫無意義的屬性名稱???首先轉到json.org並學習JSON語法,以便知道自己在做什麼。 –