使用java生成Json数据
2014-04-15 10:12
183 查看
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数据
- 使用java生成json数据
- 使用Java构造和解析Json数据的两种方法(详解二)
- 安卓使用Gson解析服务器返回Json数组,以及java服务器怎样返回list集合的Json数据(原创)
- 在JAVA中使用JSONObject生成json
- java生成json格式数据 和 java遍历json格式数据
- Java使用net.sf.json包对Json数据的操作【简而实用】
- SpringMVC中使用@RequestBody,@ResponseBody注解实现Java对象和XML/JSON数据自动转换(上)
- Java解析JSON数据 使用org.json
- JAVA中使用JSON进行数据传递
- 使用easyui将json数据生成数据表格
- JAVA中使用JSON进行数据传递
- Java 使用JSON格式和前台传数据
- 使用Java读取、创建JSON数据
- Java-封装生成JSON数据和XML数据类
- 使用Jackson转换java对象为Json数据时,Json循环的问题
- Java中使用OpenSSL生成的RSA公私钥进行数据加解密
- java生成Json工具之JsonSimple的使用
- 使用Gson反序列化Json数据失败,报错JsonSyntaxException: java.lang.IllegalStateException