前端之家收集整理的这篇文章主要介绍了
JSONStringer+JSONArray+JSONObject,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
public String testJSon() {
JSONStringer vehicle = new JSONStringer();
JSONArray jsonMembers = new JSONArray();
try {
JSONObject member1 = new JSONObject();
// vehicle.object().key("SensorData");
for (int i = 0; i < 2; i++) {
member1.put("ip","127.0.0.1");
member1.put("status_id","1");
member1.put("source_node","1");
member1.put("repeat_node","1");
member1.put("hop_count","1");
member1.put("sensor_group","1");
member1.put("sensor_no","1");
member1.put("sensor_type","1");
member1.put("sensor_status","1");
member1.put("sensor_data_1","1");
member1.put("sensor_data_2","1");
member1.put("sensor_data_3","1");
member1.put("sensor_data_4","1");
member1.put("sensor_data_5","1");
member1.put("sensor_data_6","1");
member1.put("sensor_data_hex","1");
member1.put("sensor_data_time","1");
member1.put("sensor_data_received_time","1");
member1.put("sensor_data_status","1");
jsonMembers.put(member1);
}
vehicle = vehicle.object().key("SensorData")
.value(jsonMembers.toString()).endObject();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return vehicle.toString();
}