import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class Test {
public static void main(String[] args) {
JSONObject jsonObj = new JSONObject();//创建json格式的数据
JSONArray jsonArr = new JSONArray();//json格式的数组
JSONObject jsonObjArr = new JSONObject();
try {
jsonObjArr.put("item1", "value1");
jsonObjArr.put("item2", "value2");
jsonArr.put(jsonObjArr);//将json格式的数据放到json格式的数组里
jsonObj.put("rows", jsonArr);//再将这个json格式的的数组放到最终的json对象中。
System.out.println(jsonObj.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
}
运行结果:
{"rows":[{"item2":"value2","item1":"value1"}]}