解析在网站获取的JSON数据串,获取交换机的流表信息并解析出来


package test;
//这些事导入的一些包,需要引用一些jar才行
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class HttpTest {

	// 获取带有参数的json串
	public static String getHttpResult(String url, String parm) {

		HttpClient httpclient = new DefaultHttpClient();
		HttpGet httpget = new HttpGet(url + parm);
		String json = null;
		try {
			HttpResponse response = httpclient.execute(httpget);
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				json = EntityUtils.toString(entity, "UTF-8").trim();

			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			httpget.abort();
		}
		return json;
	}

	/* 获取没有参数的json串
	public static String getHttpResult(String url) {

		HttpClient httpclient = new DefaultHttpClient();
		HttpGet httpget = new HttpGet(url);
		String json = null;
		try {
			HttpResponse response = httpclient.execute(httpget);
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				json = EntityUtils.toString(entity, "UTF-8").trim();

			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			httpget.abort();
		}
		return json;
	}
	*/
	
	//此方法用来获取某个交换机的流表信息
	public static void ParseJson(String url,String switchid,String jsonString) {
		JSONObject jsonobj = JSONObject.fromObject(jsonString); // 将json字符串转换成jsonObject对象
		JSONArray flowsOfSwitch=jsonobj.getJSONArray(switchid);//把交换机parm号的流表信息转换成Json数组
		System.out.println("交换机"+switchid+"的流表信息如下:");
		for (int i = 0; i < flowsOfSwitch.size(); i++) {//依次取出1号交换机的每一个流表
			int j=i+1;
			System.out.println("第"+j+"条流表:");
			System.out.println("actions:");
			System.out.println("actions:"+flowsOfSwitch.getJSONObject(0).getJSONArray("actions").get(0));
			System.out.println("idle_timeout:"+flowsOfSwitch.getJSONObject(0).get("idle_timeout"));
			System.out.println("cookie:"+flowsOfSwitch.getJSONObject(0).get("cookie"));
			System.out.println("packet_count:"+flowsOfSwitch.getJSONObject(0).get("packet_count"));
			System.out.println("hard_timeout:"+flowsOfSwitch.getJSONObject(0).get("hard_timeout"));
			System.out.println("byte_count:"+flowsOfSwitch.getJSONObject(0).get("byte_count"));
			System.out.println("duration_sec:"+flowsOfSwitch.getJSONObject(0).get("duration_sec"));
			System.out.println("duration_nsec:"+flowsOfSwitch.getJSONObject(0).get("duration_nsec"));
			System.out.println("priority:"+flowsOfSwitch.getJSONObject(0).get("priority"));
			System.out.println("length:"+flowsOfSwitch.getJSONObject(0).get("length"));
			System.out.println("flags:"+flowsOfSwitch.getJSONObject(0).get("flags"));
			System.out.println("table_id:"+flowsOfSwitch.getJSONObject(0).get("table_id"));
			System.out.println("match:");
			System.out.println("dl_dst:"+flowsOfSwitch.getJSONObject(0).getJSONObject("match").get("dl_dst"));
			System.out.println("dl_src:"+flowsOfSwitch.getJSONObject(0).getJSONObject("match").get("dl_src"));
			System.out.println("in_port:"+flowsOfSwitch.getJSONObject(0).getJSONObject("match").get("in_port"));
		}
	
	
	}
	
	

	/*
	 * 依次取出JSONArray中的值 private static void getJsonArrayItem(JSONArray array) { for
	 * (int i=0; i<array.size(); i++) { System.out.println(array.get(i)); } }
	 */

	public static void main(String args[]) {

		/* 获取网络拓扑中的所有交换机,第一次返回的是json串,第二次返回的是解析后的数组
		String url = "http://192.168.177.129:8080/stats/switches";
		String result = getHttpResult(url);
		JSONArray jsonarray = JSONArray.fromObject(result);
		System.out.println("未经加工的json数组:" + jsonarray);// 返回或取得数据,也就是json串
		for (int i = 0; i < jsonarray.size(); i++) {
			System.out.println("解析出来的json数组中的元素:" + jsonarray.get(i));// 返回解析好的json内的数据,其实获得的是一个数组,get(index)就是获取数组中的第几位
		}
*/
		
		/* 获取1号交换机的所有流表信息
		String parm = "1";
		String url1 = "http://192.168.177.130:8080/stats/flow/";
		String jsonString = getHttpResult(url1, parm);
		System.out.println("未经解析的交换机1的流表信息:" + jsonString);
		*/
		
		String switchid1 = "1";
		String url1 = "http://192.168.177.130:8080/stats/flow/";
		String jsonString1 = getHttpResult(url1, switchid1);
		ParseJson(url1,switchid1,jsonString1);
		
		

	}

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值