這是JSON文件。我想讓java可以產生像這樣的json。只要忽略價值,我想要的是json的結構。如何在java中構造多個對象和JSON數組android
{
"Car": [
{
"CarId": 123,
"Status": "Ok"
},
{
"CarId": 124,
"Status": "ok"
}
],
"Motor": [
{
"MotorId": 3,
"DriverId": 174
},
{
"MotorId": 3,
"DriverId": 174
}
],
"Bus": [
{
"BusId": 8,
"Status": 3
},
{
"BusId": 9,
"Status": 2
}
]
}
這是我的java代碼。
JSONObject motorObject = new JSONObject();
JSONObject busObject = new JSONObject();
JSONObject carObject = new JSONObject();
JSONArray motorArray = new JSONArray();
JSONArray busArray = new JSONArray();
JSONArray carArray = new JSONArray();
motorArray.put(motorTracks.getJSONObject());
busArray.put(buss.getJSONObject());
try
{
motorObject.put("Motor",motorArray);
busObject.put("Bus",busArray);
carArray.put(MotorObject);
carArray.put(stepObject);
carObject.put("Car",dataArray);
} catch (JSONException e)
{
e.printStackTrace();
}
輸出是:
{
"Car": [
{
"Motor": [
{
"MotorId": 0,
"DriverId": 0
}
]
},
{
"Bus": [
{
"BusId": 0,
"Status": 0
}
]
}
]
}
對於值,也沒關係,只是忽略的價值,但我想我怎樣才能得到結構是怎樣的JSON文件。
謝謝,我會嘗試。我可以知道爲什麼你把值4在rootObject.toString(4)? –
它將顯示JSON結構logcat中的輸出。 您可以嘗試使用值4,也可以不使用4,並可以看到差異。 –
好的..非常感謝..工作..而我嘗試實現我的代碼.. –