我想這樣做來創建一個像下面這樣的json對象。爲什麼原始數組不允許添加到GSON的JSON結構中
JsonObject request = new JsonObject();
request.addProperty("requestid", UUID.randomUUID().toString());
request.addProperty("type", "hotel");
request.addProperty("markups", new double[]{1.0,2.0,3.0}); // This says "The method addProperty(String, String) in the type JsonObject is not applicable for the arguments (String, double[])"
request.add("markups", new double[]{1.0,2.0,3.0});// This says "The method add(String, JsonElement) in the type JsonObject is not applicable for the arguments (String, double[])"
JSON對象:
{
"requestid": "05afcd81-9c59-4a21-a61e-ae48fda6bdd0",
"type": "hotel",
"markups": [1.0,2.0,3.0]
}
請注意,這是不是,fromJson和的toJSON事情。它是JSON創建和讀取對象不是轉換。 那麼,我怎樣才能做到這一點與上述實施。
它不清楚你在問什麼。它明顯的Gson的'JsonObject'沒有你嘗試使用的方法。我猜你需要閱讀文檔並查看'JsonArray'' –
這就是真正的大腦。我也試過JsonArrays。但它只允許添加JSONElements嗎?但我想要一種添加基元數組的方法。我的意思是,如果我們能夠做到這一點,當我們正在轉換。那麼爲什麼它在創建時不被允許? – namalfernandolk
*「爲什麼在創建時不允許?」* - 向作者提問! –