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"}]}
java组装json java构造json格式
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。

提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章