-4

How to create this kind of json in Java & how to parse it

[["Car","Bike","Bus"],["Road","Footpath","Highway"],["Horn","Sound","Pollution"]]

Im very new to java. some one please help

4

2 に答える 2

2

org.json Java パーサーを使用すると、JSON を次のように作成できます。

JSONArray jsonObj1 = new JSONArray();
jsonObj1.put("Car").put("Bike").put("Bus");

System.out.println(jsonObj1); // ["Car","Bike","Bus"]

JSONArray jsonObj2 = new JSONArray();
jsonObj2.put("Road").put("Footpath").put("Highway");

System.out.println(jsonObj2); // ["Road","Footpath","Highway"]

JSONArray jsonObj3 = new JSONArray();
jsonObj3.put("Horn").put("Sound").put("Sound");

System.out.println(jsonObj3); // ["Horn","Sound","Pollution"]

JSONArray jsonRoot = new JSONArray();
jsonRoot.put(jsonObj1).put(jsonObj2).put(jsonObj3);

System.out.println(jsonRoot);
// prints: [["Car","Bike","Bus"],["Road","Footpath","Highway"],["Horn","Sound","Pollution"]]

JSON文字列の逆シリアル化も非常に簡単です

String jsonString = jsonRoot.toString();
JSONArray jsonParsedRoot = new JSONArray(jsonString);
System.out.println(jsonParsedRoot.getJSONArray(0).getString(1)); // Bike
于 2013-08-20T22:29:48.937 に答える