如何在java中构造多个对象和JSON数组android

问题描述:

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

使用此代码,并享受:)

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

好的..非常感谢..工作..而我尝试实现我的代码.. –

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