"1" : {
"Class': "group"
"text": "test1"
}
"2" : {
"Class': "group"
"text": "test1"
}
我如何在Java中創建的JSONObject想要的JSON字符串的結構,它有這個JSON表示?
"1" : {
"Class': "group"
"text": "test1"
}
"2" : {
"Class': "group"
"text": "test1"
}
我如何在Java中創建的JSONObject想要的JSON字符串的結構,它有這個JSON表示?
使用JSONObject
從net.sf.json.JSONObject
類是這樣的:
JSONObject jsonObject=new JSONObject();
JSONObject childObject=new JSONObject();
childObject.put("class", "group");
childObject.put("text", "test1");
jsonObject.put("1", childObject);
jsonObject.put("2", childObject);
System.out.println(jsonObject.toString());
您可以直接使用字符串創建結構,也可以使用一個將以更加面向對象的方式創建JSON對象的庫。
查看https://stackoverflow.com/questions/338586/a-better-java-json-library瞭解各種JSON庫及其優缺點的一些討論。
您也可以隨時使用JSON的官方圖書館去:http://json.org/java/
我寫了類似的路線,但是有錯誤 childNode.put( 「類」 ,element.className()); childNode.put(「text」,element.ownText()); group.put(i,childNode); 因爲「我」是一個整數,如何解決它? – hkguile
已修復。謝謝 .... – hkguile