smart-json的简单使用(一)

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 + ")}";
	}
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Smart Flow Design 是一个用于流程建模和流程管理的工具。要使用 Smart Flow Design,您可以按照以下步骤进行操作: 1. 注册和登录:在 Smart Flow Design 的官方网站上注册一个账号,并使用您的凭据登录到平台上。 2. 创建新流程:在平台上,您可以创建新的流程图。点击"创建新流程"按钮或相应的选项,开始一个新的流程建模项目。 3. 添加流程元素:在流程图中,您可以添加不同的元素来表示不同的步骤或活动。例如,您可以添加开始节点、结束节点、任务节点、判断节点等。 4. 连接流程元素:使用箭头或线条将不同的流程元素连接起来,以表示它们之间的流转关系和顺序。 5. 定义流程细节:对于每个流程元素,您可以添加相关的细节和说明,例如任务名称、负责人、截止日期等。 6. 设计流程逻辑:使用条件和分支语句,您可以设计流程的逻辑,使得流程能够根据不同的条件执行不同的操作。 7. 保存和共享流程:完成流程建模后,您可以保存流程图,并选择是否与团队成员共享。您还可以导出流程图为不同的文件格式,如图片或PDF。 8. 执行和管理流程:一旦流程建模完成,您可以使用 Smart Flow Design 来执行和管理流程。您可以跟踪流程的进度、分配任务、更新状态等。 以上是 Smart Flow Design 的基本使用步骤,您可以根据您的具体需求和业务流程进行定制和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值