【第4篇】通过Json-lib把Json转成Map数据

package ivyy.taobao.com.entity;

import java.io.Serializable;

/**
 *@Author:liangjl
 *@Date:2014-12-19
 *@Version:1.0
 *@Description:
 */
public class Student implements Serializable{
	private Integer age;
	private String sex;
	private String userName;
	private String birthday;
	private String address;
	private String email;
	
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getBirthday() {
		return birthday;
	}
	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
}
package ivyy.taobao.com.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.OutputStreamWriter;
import java.io.Writer;

/**
 *@Author:liangjilong
 *@Date:2015-1-5
 *@Version:1.0
 *@Description:
 */
public class IoUtils {

	/**
	 * 读文件流
	 * 
	 * @param formPath从哪里读取的文件路径
	 * @return
	 */
	public static String reader(String formPath) {
		String content="";
		FileReader read = null;
		BufferedReader reader = null;
		try {
			read = new FileReader(new File(formPath));
			reader = new BufferedReader(read);
			StringBuffer buffer = new StringBuffer("");
			content = reader.readLine();
			while (content != null) {
				buffer.append(content).append("\n");
				content = reader.readLine();
			}
			return content = buffer.toString();// 返回
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (reader != null)
					reader.close();
				if (read != null)
					read.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return "";// 没值就返回空
	}
	
	/**
	 * 处理文件写
	 * @param content
	 * @param htmlPath
	 * @return
	 */
	public static boolean write(String content, String htmlPath) {  
        boolean flag = true;  
        try {  
            Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlPath), "utf-8"));  
            out.write("\n" + content);  
            out.close();  
        } catch (Exception ex) {  
            ex.printStackTrace();  
            return false;  
        }  
        return flag;  
    } 
}

package ivyy.taobao.com.domain.jsonlib;

import ivyy.taobao.com.entity.Student;
import ivyy.taobao.com.utils.IoUtils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import net.sf.json.JSONObject;

/**
 *@DEMO:json
 *@Java:Test2.java
 *@Date:2015-1-18下午2:45:02
 *@Author:liangjilong
 *@Email:jilongliang@sina.com
 *@Weibo:http://weibo.com/jilongliang
 *@Version:1.0
 *@Description:通过Json-lib把Json转成Map数据
 */
public class Json2Map {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		
		//1、获取当前json文本的路径
		String formPath=Json2Map.class.getClassLoader().getResource("ivyy/taobao/com/domain/jsonlib/json2map.txt").getPath();
		//2、读取文件流的文件数据
		String json=IoUtils.reader(formPath);
		//3、格式化数据
		JSONObject jsonObject = JSONObject.fromObject(json);
		//4、创建一个Map
		Map<String, Class<?>> clazzMap = new HashMap<String, Class<?>>();
		clazzMap.put("result", String[].class);
		clazzMap.put("Student1", Student.class);
		clazzMap.put("Student2", Student.class);
		//5、把Json转换成Map
		Map<String, ?> mapBean = (Map) JSONObject.toBean(jsonObject, Map.class,clazzMap);

		Set<String> set = mapBean.keySet();
		//6、遍历Json数据
		Iterator<String> iter = set.iterator();
		while (iter.hasNext()) {
			String key = iter.next();
			System.out.print(key + ":" + mapBean.get(key).toString()+"\t");
		}
	}

}


{
    "result": [
        "Student1",
        "Student2"
    ],
    "Student1": {
        "address": "广东省云浮市东升布艺",
        "birthday": {
            "birthday": "1988-8-18"
        },
        "email": "jilongliang@sina.com",
        "id": 1,
        "userName": "jilongliang"
    },
    "Student2": {
        "address": "http://weibo.com/resourceljl",
        "birthday": {
            "birthday": "1988-8-18"
        },
        "email": "jilongliang@sina.com",
        "id": 1,
        "userName": "liangjilong"
    },
    "userName": "东升布艺",
    "bool": true
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱学习的蹭蹭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值