2017-04-10 65 views
2

這是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文件。

回答

1

使用此代碼,並享受:)

private void createJsonStructure() { 

    try 
    { 
     JSONObject rootObject = new JSONObject(); 

     JSONArray carArr = new JSONArray(); 
     for (int i = 0; i < 2 ; i++) 
     { 
      JSONObject jsonObject = new JSONObject(); 
      jsonObject.put("CarId", "123"); 
      jsonObject.put("Status", "Ok"); 
      carArr.put(jsonObject); 
     } 
     rootObject.put("Car", carArr); 


     JSONArray motorArr = new JSONArray(); 
     for (int i = 0; i < 2 ; i++) 
     { 
      JSONObject jsonObject = new JSONObject(); 
      jsonObject.put("MotorId", "123"); 
      jsonObject.put("Status", "Ok"); 
      motorArr.put(jsonObject); 
     } 
     rootObject.put("Motor", motorArr); 


     JSONArray busArr = new JSONArray(); 
     for (int i = 0; i < 2 ; i++) 
     { 
      JSONObject jsonObject = new JSONObject(); 
      jsonObject.put("BusId", "123"); 
      jsonObject.put("Status", "Ok"); 
      busArr.put(jsonObject); 
     } 
     rootObject.put("Bus", busArr); 

     Log.e("JsonObject", rootObject.toString(4)); 

    } 
    catch (Exception ex) 
    { 
     ex.printStackTrace(); 
    } 
} 
+0

謝謝,我會嘗試。我可以知道爲什麼你把值4在rootObject.toString(4)? –

+0

它將顯示JSON結構logcat中的輸出。 您可以嘗試使用值4,也可以不使用4,並可以看到差異。 –

+0

好的..非常感謝..工作..而我嘗試實現我的代碼.. –

1
JSONObject motorObject = new JSONObject(); 
    JSONObject busObject = new JSONObject(); 
    JSONObject carObject = new JSONObject(); 
    JSONObject wholeObject =new JSONObject(); 

    JSONArray motorArray = new JSONArray(); 
    JSONArray busArray = new JSONArray(); 
    JSONArray carArray = new JSONArray(); 

    motorArray.put(motorTracks.getJSONObject()); 
    busArray.put(buss.getJSONObject()); 
    carArray.put(car.getJSONObject()); 

    try 
    { 
     wholeObject.put("Motor",motorArray); 
     wholeObject.put("Bus",busArray); 
     wholeObject.put("Car",carArray); 
     System.out.println(wholeObject); 
    } 
    catch (JSONException e) 
    { 
     e.printStackTrace(); 
    } 
+0

也工作..謝謝... –

+0

我試着把jsonObject.put(「BusId」,null); ...但輸出不顯示BusId –

+0

jsonObject不會考慮一個key的空值。試試把空字符串「」 – MohanaPriyan

相關問題