悠然哈哈哈

导航

json、JSONObject、JSONArray的应用

type.java

package jiekou.duixiang;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import util.Constant;

public class Type {

private String id;
private String name;
private String image;
private String level;
private Date time;

public Type() {
super();
}
public Type(Object[] obj) {
super();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
this.id = String.valueOf(obj[0]);
this.name = String.valueOf(obj[1]);
this.image = Constant.IMG_PATH+String.valueOf(obj[2]);
this.level=String.valueOf(obj[3]);
try {
this.time = format.parse(String.valueOf(obj[4]));
} catch (ParseException e) {
this.time = null;
e.printStackTrace();
}
}
@Override
public String toString(){
StringBuilder sb = new StringBuilder();
sb.append("{");
sb.append(id).append(",").append(name).append(",").append(image).append(",").append(level).append(",").append(time);
sb.append("}");
return sb.toString();
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getLevel() {
return level;
}
public void setLevel(String level) {
this.level = level;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}

}

 

ResponseResult.java

package jiekou.tool;

public class ResponseResult<T> {

/**
* 消息类型
*/
public String messageType;
/**
* 消息对象
*/
public T info;
public String getMessageType() {
return messageType;
}
public void setMessageType(String messageType) {
this.messageType = messageType;
}
public T getInfo() {
return info;
}
public void setInfo(T info) {
this.info = info;
}
public ResponseResult(String messageType, T info) {
super();
this.messageType = messageType;
this.info = info;
}
public ResponseResult(T info) {
super();
this.info = info;
}
public ResponseResult(String messageType) {
super();
this.messageType = messageType;
}
@Override
public String toString(){
StringBuilder sb = new StringBuilder();
sb.append("{");
sb.append(messageType).append(",").append(info);
sb.append("}");
return sb.toString();
}
}

 

Test.java

package test;

import java.util.ArrayList;
import java.util.List;

import jiekou.duixiang.Type;
import jiekou.duixiang.YongHu;
import jiekou.tool.ResponseResult;
import net.sf.json.JSONArray;

import org.apache.commons.codec.digest.DigestUtils;

import util.Constant;
import dao.impl.CommentDaoImpl;
import dao.impl.InstructionDaoImpl;
import dao.impl.TreeNodeDaoImpl;
import dao.impl.UserDaoImpl;
import entity.User;

public class test {
public static String getMd5Password(String userPwd, String salt) {
userPwd=DigestUtils.sha1Hex(userPwd);
userPwd=DigestUtils.md5Hex(userPwd+salt);
return userPwd;
}

public static void main(String[] args) {
/******************对象************************/
UserDaoImpl daoImpl=new UserDaoImpl();
InstructionDaoImpl instructionDaoImpl=new InstructionDaoImpl();
CommentDaoImpl commentDaoImpl=new CommentDaoImpl();
YongHu yh=new YongHu();
String userStr="admin";
String password="123123";
String token="1";
User user=daoImpl.getfindUserAccount(userStr);
if(user!=null){
password=getMd5Password(password,String.valueOf(user.getSalt()));
if(!user.getUserPwd().equals(password)){
}else{
int upload=instructionDaoImpl.getUpload(user.getUserAccount());
int collection=0;
int comment=commentDaoImpl.getComment(user.getUserAccount());
yh=new YongHu(token, user.getUserAccount(), Constant.IMG_PATH+user.getUserImage(), user.getUserName(),
user.getEmail(), user.getPhone(), user.getSex(), user.getProvince(),
user.getCity(), user.getRegisterTime(), user.getQq(), user.getAddress(), user.getBirthday(), user.getRemark(),
user.getSign(), collection, upload, comment);
JSONArray jsonArray =null;
ResponseResult<YongHu> a=new ResponseResult<YongHu>("0",yh);
List<String> types=new ArrayList<String>();
types.add(a.toString());
jsonArray=JSONArray.fromObject(types);
System.out.println(jsonArray.toString());
}
}else{
}
/******************数组************************/
JSONArray jsonArray =null;
TreeNodeDaoImpl daoImpl2=new TreeNodeDaoImpl();
String update_time="2015-04-03";
List<?> list=daoImpl2.getTime(update_time);
Type type=new Type();
List<String> types=new ArrayList<String>();
for(int i=0;i<list.size();i++) {
Object[] obj = (Object[]) list.get(i);
type=new Type(obj);
types.add(type.toString());
}
jsonArray=JSONArray.fromObject(types);
System.out.println(jsonArray.toString());

}
}

 

说明:

 把JSONArray[{"id":"1","name":"啦啦","age":"18"},{"id":"2","name":"慢慢","age":"19"}] 转成 [{1,啦啦,18},{1,慢慢,19}]把key去掉,只显示value 

 

posted on 2015-05-05 17:03  悠然886  阅读(244)  评论(0编辑  收藏  举报