java 生成json与解析
2015-12-21 10:59
519 查看
在JAVA中构造和解析JSON我用的是org.json,附件为相应的org.json.jar.
下面是两个函数,一个是创建JSON,一个是从文本构造JSON并解析之。
创建json
//construct json and output it
public String jsonTest() throws JSONException{
JSONObject json=new JSONObject();
JSONArray jsonMembers = new JSONArray();
JSONObject member1 = new JSONObject();
member1.put("loginname", "zhangfan");
member1.put("password", "userpass");
member1.put("email","10371443@qq.com");
member1.put("sign_date", "2007-06-12");
jsonMembers.put(member1);
JSONObject member2 = new JSONObject();
member2.put("loginname", "zf");
member2.put("password", "userpass");
member2.put("email","8223939@qq.com");
member2.put("sign_date", "2008-07-16");
jsonMembers.put(member2);
json.put("users", jsonMembers);
return json.toString();
}
解析json
Java代码
//construct json from String and resolve it.
public String jsonTest2() throws JSONException{
String jsonString="{\"users\":[{\"loginname\":\"zhangfan\",\"password\":\"userpass\",\"email\":\"10371443@qq.com\"},{\"loginname\":\"zf\",\"password\":\"userpass\",\"email\":\"822393@qq.com\"}]}";
JSONObject json= new JSONObject(jsonString);
JSONArray jsonArray=json.getJSONArray("users");
String loginNames="loginname list:";
for(int i=0;i<jsonArray.length();i++){
JSONObject user=(JSONObject) jsonArray.get(i);
String userName=(String) user.get("loginname");
if(i==jsonArray.length()-1){
loginNames+=userName;
}else{
loginNames+=userName+",";
}
}
return loginNames;
}
转载:http://zhangfan822.iteye.com/blog/1880830
下面是两个函数,一个是创建JSON,一个是从文本构造JSON并解析之。
创建json
//construct json and output it
public String jsonTest() throws JSONException{
JSONObject json=new JSONObject();
JSONArray jsonMembers = new JSONArray();
JSONObject member1 = new JSONObject();
member1.put("loginname", "zhangfan");
member1.put("password", "userpass");
member1.put("email","10371443@qq.com");
member1.put("sign_date", "2007-06-12");
jsonMembers.put(member1);
JSONObject member2 = new JSONObject();
member2.put("loginname", "zf");
member2.put("password", "userpass");
member2.put("email","8223939@qq.com");
member2.put("sign_date", "2008-07-16");
jsonMembers.put(member2);
json.put("users", jsonMembers);
return json.toString();
}
解析json
Java代码
//construct json from String and resolve it.
public String jsonTest2() throws JSONException{
String jsonString="{\"users\":[{\"loginname\":\"zhangfan\",\"password\":\"userpass\",\"email\":\"10371443@qq.com\"},{\"loginname\":\"zf\",\"password\":\"userpass\",\"email\":\"822393@qq.com\"}]}";
JSONObject json= new JSONObject(jsonString);
JSONArray jsonArray=json.getJSONArray("users");
String loginNames="loginname list:";
for(int i=0;i<jsonArray.length();i++){
JSONObject user=(JSONObject) jsonArray.get(i);
String userName=(String) user.get("loginname");
if(i==jsonArray.length()-1){
loginNames+=userName;
}else{
loginNames+=userName+",";
}
}
return loginNames;
}
转载:http://zhangfan822.iteye.com/blog/1880830
相关文章推荐
- 读取excel,利用java反射组装实体集合
- 你没见过的通俗spring mvc~~~有好戏
- java 支付宝支付alipay
- Ajax+Spring MVC实现跨域请求(JSONP)JSONP 跨域
- Struts2 Action名称的搜索顺序
- elasticsearch java client
- java框架问题杂想
- java继承示例
- eclipse启动时问题
- java.lang.Runtime类总结
- java 格式化 例子
- Java 多线程 并发编程
- Spring4+Struts2+Hibernate5报错:Access denied for user 'root'@'localhost' (using password: YES)
- MyEclipse2013常设置(开发必备)
- 精通Spring 之 注解【<context:property-placeholder/>】
- springmvc+mybatis+velocity配置
- 使用Retrofit和RxJava
- java实现栈
- java实现定时任务的三种方法
- 修改Spring默认的单例模式