java json字符串处理

使用Java操作JSON字符串对象

http://www.blogjava.net/Werther/archive/2010/01/20/310262.html


Java解析Json(org.json,json-lib)

http://ysj5125094.iteye.com/blog/1633238


代码样例:

// 输出结果为{"version": 4,"addr": "192.160.1.11"}
	private static void test1()
	{
		
		String s = "{\"internal_1\": [{\"version\": 4,\"addr\": \"192.160.1.11\"}]}";
		String regex = ".+?\\[(.+?)\\].+?";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(s);
		if (matcher.matches()) {
			String group = matcher.group(1);
			System.out.println(group);
		}else {
			System.out.println("no matches!!");
		}
	}
	
	// 输出结果为{"Done":1,"ReturnType":1,"Msg":"HELLO,上海"}
	private static void test2()
	{
		String s="TestJsonResponse{TestJsonResult={\"Done\":1,\"ReturnType\":1,\"Msg\":\"HELLO,上海\"};}";
		String regex = ".+?\\=(.+?)\\;.+?";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(s);
		if (matcher.matches()) {
			String group = matcher.group(1);
			System.out.println(group);
		}else {
			System.out.println("no matches!!");
		}
	}
	
	//结果为HELLO,上海
	private static void test3() 
	{
  
		/*JSONObject jsonObject = new JSONObject();   
		try {
			jsonObject.put("a", 1);
			jsonObject.put("b", 1.1);   
			jsonObject.put("c", 1L);   
			jsonObject.put("d", "test");   
			jsonObject.put("e", true);   
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}   
		
		System.out.println(jsonObject); // 输出{"d":"test","e":true,"b":1.1,"c":1,"a":1}
		try {
			System.out.println(jsonObject.getString("d"));// 输出test
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} */
		
		String s="{\"Done\":1,\"ReturnType\":1,\"Msg\":\"HELLO,上海\"}";
		try {
			JSONObject jsonObject2 = new JSONObject(s); 
			System.out.println(jsonObject2.getString("Msg")); // 输出HELLO,上海
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值