使用java生成Json数据

import org.json.JSONArray;


import org.json.JSONException;


import org.json.JSONObject;


 


public class TestJson {


   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) {


         // TODO Auto-generated catch block


         e.printStackTrace();


      }


   }


}


 


最终程序生成的数据格式就如下:


{"rows":[{"item1":"value1","item2":"value2"}]}

你可能感兴趣的:(json,数据)