您的位置:首页 > 编程语言 > Java开发

使用java生成Json数据

2012-10-10 11:52 423 查看
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"}]}

原文:http://blog.csdn.net/tarena_xhf/article/details/7405636
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: