`

json与对象互转-1

 
阅读更多

json与对象互转-1

json文件请看:json与对象互转-1.json

import java.util.List;
/**
 *	用户打卡信息
 */
public class Checkindata {
	private String userid;
	private String groupname;
	private String checkin_type;//外出打卡/上下班打卡
	private String exception_type;
	private long checkin_time;//1531373790
	private String location_title;
	private String location_detail;
	private String wifiname;
	private String notes;
	private String wifimac;
	private List<String> mediaids;
	public Checkindata(){} ;
	public Checkindata(String userid,String groupname,String checkin_type,String exception_type,
			long checkin_time,String location_title,String location_detail,String wifiname,String notes,
			String wifimac,List<String> mediaids) {
		super();
		this.userid = userid;
		this.groupname = groupname;
		this.checkin_type = checkin_type;
		this.exception_type = exception_type;
		this.checkin_time = checkin_time;
		this.location_title = location_title;
		this.location_detail = location_detail;
		this.wifiname = wifiname;
		this.notes = notes;
		this.wifimac = wifimac;
		this.mediaids = mediaids;
	}
	//省略N个SET&GET
}

 

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import net.sf.json.JSONObject;
import com.google.gson.Gson;
public class CheckinList {
	private int errcode;
	private String errmsg;
	private List<Checkindata> checkindata;
	public CheckinList(List<Checkindata> checkindata,int errcode,String errmsg) {
		super();
		this.checkindata = checkindata;
		this.errcode = errcode;
		this.errmsg = errmsg;
	}
	public CheckinList() {}
	//省略N个SET&GET
	
	public static void main(String[] args) throws IOException {
		String file = "json与对象互转-1.json";
		File f=new File(file);
		BufferedReader br=new BufferedReader(new FileReader(f));
		String lineinfo="";
		StringBuffer sb = new StringBuffer();
		while((lineinfo = br.readLine()) != null){
			 sb.append(lineinfo);
		}
		br.close();
		System.out.println(sb.toString());
		Gson g1 = new Gson();
		CheckinList c1 = g1.fromJson(sb.toString(), CheckinList.class);
		System.out.println(c1.getErrcode()+","+c1.getErrmsg()+":"+c1.getDatas());
		for(Checkindata c : c1.getDatas()){
			System.out.println(c.getCheckin_time()+","+c.getLocation_title()+","+c.getUserid()+","+c.getLocation_detail());
		}
		System.out.println("1-------------------------------------");
		JSONObject jsonObj =  JSONObject.fromObject(sb.toString()); 
		String checkindata = jsonObj.getString("checkindata");
		System.out.println(checkindata);
		System.out.println("2-------------------------------------");
		String checkin = checkindata.substring(1, checkindata.length() - 1);
		System.out.println(checkin);
		System.out.println("3-------------------------------------");
		JSONObject jsonDatas =  JSONObject.fromObject(checkin);
		System.out.println(jsonDatas.toString());
		System.out.println("4-------------------------------------");
		Gson gson = new Gson();
		Checkindata c = gson.fromJson(jsonDatas.toString(), Checkindata.class);
		System.out.println(c.getCheckin_time()+","+c.getLocation_title());
	}
}

 json与对象互转-1.json

{
    "errcode":0,
    "errmsg":"ok",
    "checkindata":[
        {
            "userid":"MinYongCheng",
            "groupname":"",
            "checkin_type":"外出打卡",
            "exception_type":"",
            "checkin_time":1531373790,
            "location_title":"合肥南站",
            "location_detail":"安徽省合肥市包河区徽州大道与绕城高速交叉口东北",
            "wifiname":"",
            "notes":"",
            "wifimac":"",
            "mediaids":[

            ]
        },
        {
            "userid":"MinYongCheng",
            "groupname":"",
            "checkin_type":"外出打卡",
            "exception_type":"",
            "checkin_time":1531382536,
            "location_title":"金竹坑",
            "location_detail":"江西省上饶市婺源县秋口镇金竹坑",
            "wifiname":"",
            "notes":"",
            "wifimac":"",
            "mediaids":[

            ]
        }
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics