smart-json是一个高性能的json类库:

本实例主要讲了:1.如何将xml字符串转换为Json. 2.如何将json转换为对象.

package com.ilucky.json.smart;

import com.ilucky.json.smart.model.Body;
import com.ilucky.json.smart.model.Head;
import com.ilucky.json.smart.model.Message;

import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
import net.minidev.json.JSONValue;

/**
 * @author IluckySi
 * @date 20140801
 */
public class MainTest {

		public static void main(String[] args) {
		/**
		<head>
			<ip>127.0.0.1>
			<mac>12-23-34-45-56-67</mac>
		</head>
		<body>
			<cpu>60%</cpu>
			<memory>70%</memory>
			<disk>80%</disk>
		</body>
		 */
			//将xml信息放到JSON字符串中.
			String jsonString = convertString2Json();
			System.out.println(jsonString);
			
			//将JSON字符串中转换为对象.
			Message message = convertJson2Object(jsonString);
			System.out.println(message);
		}
		
		//将xml信息放到JSON字符串中.
		public static String convertString2Json() {
			//1.声明一个JSON数组.
			JSONArray host = new JSONArray();
			
			//2.存放xml文件头信息.
			JSONObject head = new JSONObject();
			JSONObject headInfo = new JSONObject();
			headInfo.put("ip", "127.0.0.1");
			headInfo.put("mac", "12-23-34-45-56-67");
			head.put("head", headInfo);
			host.add(head);
			
			//3.存放xml文件体信息.
			JSONObject body = new JSONObject();
			JSONObject bodyInfo = new JSONObject();
			bodyInfo.put("cpu", "60");
			bodyInfo.put("memory", "70");
			bodyInfo.put("disk", "80");
			body.put("body", bodyInfo);
			host.add(body);
			
			//4.将JSONA数组转换为字符串.
			String jsonString = host.toJSONString();
			return jsonString;
		}
		
		//将JSON字符串中转换为对象.
		public static Message convertJson2Object(String jsonString) {
			//1.将JSON字符串转换为JSON数组.
			JSONArray jsonArray = (JSONArray) JSONValue.parse(jsonString);
			Message message = new Message();
			
			//2.遍历JSON数组.
			for (int i = 0; jsonArray != null && i < jsonArray.size(); i++) {
				JSONObject jsonObject = (JSONObject) jsonArray.get(i);
				if(jsonObject.containsKey("head")) {
					JSONObject childJsonObject = (JSONObject)jsonObject.get("head");
					Head head = new Head();
					head.setIp(childJsonObject.get("ip").toString());
					head.setMac(childJsonObject.get("mac").toString());
					message.setHead(head);
				} else if(jsonObject.containsKey("body")) {
					JSONObject childJsonObject = (JSONObject)jsonObject.get("body");
					Body body = new Body();
					body.setCpu(childJsonObject.get("cpu").toString());
					body.setMemory(childJsonObject.get("memory").toString());
					body.setDisk(childJsonObject.get("disk").toString());
					message.setBody(body);
				}
			}
			return message;
	}
}
/**
 输出结果:
[{"head":{"mac":"12-23-34-45-56-67","ip":"127.0.0.1"}},{"body":{"disk":"80","cpu":"60","memory":"70"}}]
message = {head = (ip = 127.0.0.1, mac = 12-23-34-45-56-67), body = (cpu = 60, memory = 70, disk = 80)}
 */
package com.ilucky.json.smart.model;

/**
 * @author IluckySi
 * @date 20140801
 */
public class Head {

	private String ip;
	
	private String mac;

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public String getMac() {
		return mac;
	}

	public void setMac(String mac) {
		this.mac = mac;
	}
	
	public String toString() {
		return "ip = " + ip + ", mac = " + mac;
	}
}
package com.ilucky.json.smart.model;

/**
 * @author IluckySi
 * @date 20140801
 */
public class Body {

	private String cpu;
	
	private String memory;
	
	private String disk;

	public String getCpu() {
		return cpu;
	}

	public void setCpu(String cpu) {
		this.cpu = cpu;
	}

	public String getMemory() {
		return memory;
	}

	public void setMemory(String memory) {
		this.memory = memory;
	}

	public String getDisk() {
		return disk;
	}

	public void setDisk(String disk) {
		this.disk = disk;
	}
	
	public String toString() {
		return "cpu = " + cpu + ", memory = " + memory + ", disk = " + disk;
	}
}
package com.ilucky.json.smart.model;

/**
 * @author IluckySi
 * @date 20140801
 */
public class Message {

	private Head head;
	
	private Body body;

	public Head getHead() {
		return head;
	}

	public void setHead(Head head) {
		this.head = head;
	}

	public Body getBody() {
		return body;
	}

	public void setBody(Body body) {
		this.body = body;
	}
	
	public String toString() {
		return "message = {head = (" + head + "), body = (" + body + ")}";
	}
}

GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:1 个月前 )
960b763e 3 个月前
8c391e04 6 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐