0
我有一個小問題 我已經寫創建的JSONObject對象的代碼,並添加一些關於它的數據是這樣的:如何創建一個動態的JSONObject?
JSONObject outerObject = new JSONObject();
JSONArray outerArray = new JSONArray();
JSONObject innerObject1 = new JSONObject();
innerObject1.put("id", "semantic web");
innerObject1.put("url", "www.semanticweb.com");
innerObject1.put("definition", "this is the denfition of semantic web");
outerArray.put(innerObject1);
然後我再創建一個名爲innerObject2,做這樣相同的過程:
JSONObject innerObject2 = new JSONObject();
innerObject2.put("id", "ontology");
innerObject2.put("url", "www.ontology.com");
innerObject2.put("definition", "this is the denfition of ontology");
outerArray.put(innerObject2);
outerObject.put("rows", outerArray);
return outerObject.toString();
結果會有些事情是這樣的:
{"rows":[{"definition":"this is the denfition of semantic web","id":"semantic web","url":"www.semanticweb.com"},{"definition":"this is the denfition of ontology","id":"ontology","url":"www.ontology.com"}]}
我的問題是:如果什麼我想創建另一個名爲innerObject3,innerObject4,innerObject5 ....使用for循環的JSONObject? 有什麼建議嗎?
你可以使用'gson'或類似助手庫來操縱java對象到json對象。 – alijandro 2015-03-13 08:04:09