`
flashdream8
  • 浏览: 667145 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

unicode 和 汉字转换

 
阅读更多

 

// unicode转为本地
	public static String ascii2Native(String str) {
		StringBuilder sb = new StringBuilder();
		int begin = 0;
		int index = str.indexOf("\\u");
		while (index != -1) {
			sb.append(str.substring(begin, index));
			sb.append(ascii2Char(str.substring(index, index + 6)));
			begin = index + 6;
			index = str.indexOf("\\u", begin);
		}
		sb.append(str.substring(begin));
		return sb.toString();
	}

	private static char ascii2Char(String str) {
		if (str.length() != 6) {
			throw new IllegalArgumentException(
					"Ascii string of a native character must be 6 character.");
		}
		if (!"\\u".equals(str.substring(0, 2))) {
			throw new IllegalArgumentException(
					"Ascii string of a native character must start with \"\\u\".");
		}
		String tmp = str.substring(2, 4);
		int code = Integer.parseInt(tmp, 16) << 8;
		tmp = str.substring(4, 6);
		code += Integer.parseInt(tmp, 16);
		return (char) code;
	}

 

例如:

 {"WeatherForecast":[{"Date":"2013-06-18","City":{"Name":"\u6c49\u4e2d","ID":"610700"},"Weather":{"Desc":"\u591a\u4e91","Symbol":"03"},"Temperature":{"Low":"24","High":"33"},"Wind":{"Power":"","Direction":"\u5fae\u98ce","PowerTurn":"","DirectionTurn":""},"Cloth":{"Index":"1","Desc":"\u77ed\u8896\u886b\u3001\u77ed\u88d9\u3001\u77ed\u88e4\u3001\u8584\u578bT\u6064\u886b\u3001\u655e\u9886\u77ed\u8896\u68c9\u886b"},"Pollute":{"Index":"3","Desc":"\u5bf9\u7a7a\u6c14\u6c61\u67d3\u7269\u6269\u6563\u65e0\u660e\u663e\u5f71\u54cd"},"Cold":{"Index":"3","Desc":"\u5929\u6c14\u95f7\u70ed\uff0c\u8981\u9884\u9632\u70ed\u4f24\u98ce\u8f83\u6613\u5f15\u8d77\u611f\u5192"},"Traffic":{"Index":"1","Desc":"\u80fd\u89c1\u5ea6\u5f88\u597d\uff0c\u8def\u9762\u89c6\u91ce\u5f00\u9614\uff0c\u975e\u5e38\u9002\u5b9c\u9a7e\u9a76\u6c7d\u8f66\u51fa\u884c\u3002"},"Sport":{"Index":"5","Desc":"\u5929\u6c14\u95f7\u70ed\uff0c\u4e0d\u9002\u5b9c\u6237\u5916\u8fd0\u52a8"},"Tour":{"Index":"5","Desc":"\u5929\u6c14\u95f7\u70ed\uff0c\u4e0d\u9002\u5b9c\u65c5\u6e38"}},{"Date":"2013-06-19","City":{"Name":"\u6c49\u4e2d","ID":"610700"},"Weather":{"Desc":"\u591a\u4e91","Symbol":"03"},"Temperature":{"Low":"26","High":"33"},"Wind":{"Power":"","Direction":"\u5fae\u98ce","PowerTurn":"","DirectionTurn":""},"Cloth":{"Index":"1","Desc":"\u77ed\u8896\u886b\u3001\u77ed\u88d9\u3001\u77ed\u88e4\u3001\u8584\u578bT\u6064\u886b\u3001\u655e\u9886\u77ed\u8896\u68c9\u886b"},"Pollute":{"Index":"3","Desc":"\u5bf9\u7a7a\u6c14\u6c61\u67d3\u7269\u6269\u6563\u65e0\u660e\u663e\u5f71\u54cd"},"Cold":{"Index":"3","Desc":"\u5929\u6c14\u95f7\u70ed\uff0c\u8981\u9884\u9632\u70ed\u4f24\u98ce\u8f83\u6613\u5f15\u8d77\u611f\u5192"},"Traffic":{"Index":"1","Desc":"\u80fd\u89c1\u5ea6\u5f88\u597d\uff0c\u8def\u9762\u89c6\u91ce\u5f00\u9614\uff0c\u975e\u5e38\u9002\u5b9c\u9a7e\u9a76\u6c7d\u8f66\u51fa\u884c\u3002"},"Sport":{"Index":"5","Desc":"\u5929\u6c14\u95f7\u70ed\uff0c\u4e0d\u9002\u5b9c\u6237\u5916\u8fd0\u52a8"},"Tour":{"Index":"5","Desc":"\u5929\u6c14\u95f7\u70ed\uff0c\u4e0d\u9002\u5b9c\u65c5\u6e38"}},{"Date":"2013-06-20","City":{"Name":"\u6c49\u4e2d","ID":"610700"},"Weather":{"Desc":"\u591a\u4e91\u8f6c\u9635\u96e8","Symbol":"0309"},"Temperature":{"Low":"25","High":"31"},"Wind":{"Power":"","Direction":"\u5fae\u98ce","PowerTurn":"","DirectionTurn":""},"Cloth":{"Index":"1","Desc":"\u77ed\u8896\u886b\u3001\u77ed\u88d9\u3001\u77ed\u88e4\u3001\u8584\u578bT\u6064\u886b\u3001\u655e\u9886\u77ed\u8896\u68c9\u886b"},"Pollute":{"Index":"3","Desc":"\u5bf9\u7a7a\u6c14\u6c61\u67d3\u7269\u6269\u6563\u65e0\u660e\u663e\u5f71\u54cd"},"Cold":{"Index":"2","Desc":"\u73af\u5883\u6e29\u5ea6\u8f83\u9ad8\uff0c\u867d\u7136\u4e0d\u6613\u53d1\u751f\u611f\u5192\uff0c\u8981\u63d0\u9884\u9632\u7a7a\u8c03\u75c5"},"Traffic":{"Index":"4","Desc":"\u6709\u5c0f\u96e8\uff0c\u4e0d\u592a\u9002\u5b9c\u9a7e\u9a76\u6c7d\u8f66\u51fa\u884c\uff0c\u9002\u5f53\u63a7\u5236\u8f66\u901f\u3002"},"Sport":{"Index":"5","Desc":"\u5929\u6c14\u708e\u70ed\uff0c\u4e0d\u9002\u5b9c\u6237\u5916\u8fd0\u52a8"},"Tour":{"Index":"3","Desc":"\u5929\u6c14\u708e\u70ed\uff0c\u6bd4\u8f83\u9002\u5b9c\u65c5\u6e38\u4f46\u8981\u5e26\u65f1\u4f1e\u6216\u906e\u9633\u5e3d"}}]}

 转换以后

 {"WeatherForecast":[{"Date":"2013-06-18","City":{"Name":"汉中","ID":"610700"},"Weather":{"Desc":"多云","Symbol":"03"},"Temperature":{"Low":"24","High":"33"},"Wind":{"Power":"","Direction":"微风","PowerTurn":"","DirectionTurn":""},"Cloth":{"Index":"1","Desc":"短袖衫、短裙、短裤、薄型T恤衫、敞领短袖棉衫"},"Pollute":{"Index":"3","Desc":"对空气污染物扩散无明显影响"},"Cold":{"Index":"3","Desc":"天气闷热,要预防热伤风较易引起感冒"},"Traffic":{"Index":"1","Desc":"能见度很好,路面视野开阔,非常适宜驾驶汽车出行。"},"Sport":{"Index":"5","Desc":"天气闷热,不适宜户外运动"},"Tour":{"Index":"5","Desc":"天气闷热,不适宜旅游"}},{"Date":"2013-06-19","City":{"Name":"汉中","ID":"610700"},"Weather":{"Desc":"多云","Symbol":"03"},"Temperature":{"Low":"26","High":"33"},"Wind":{"Power":"","Direction":"微风","PowerTurn":"","DirectionTurn":""},"Cloth":{"Index":"1","Desc":"短袖衫、短裙、短裤、薄型T恤衫、敞领短袖棉衫"},"Pollute":{"Index":"3","Desc":"对空气污染物扩散无明显影响"},"Cold":{"Index":"3","Desc":"天气闷热,要预防热伤风较易引起感冒"},"Traffic":{"Index":"1","Desc":"能见度很好,路面视野开阔,非常适宜驾驶汽车出行。"},"Sport":{"Index":"5","Desc":"天气闷热,不适宜户外运动"},"Tour":{"Index":"5","Desc":"天气闷热,不适宜旅游"}},{"Date":"2013-06-20","City":{"Name":"汉中","ID":"610700"},"Weather":{"Desc":"多云转阵雨","Symbol":"0309"},"Temperature":{"Low":"25","High":"31"},"Wind":{"Power":"","Direction":"微风","PowerTurn":"","DirectionTurn":""},"Cloth":{"Index":"1","Desc":"短袖衫、短裙、短裤、薄型T恤衫、敞领短袖棉衫"},"Pollute":{"Index":"3","Desc":"对空气污染物扩散无明显影响"},"Cold":{"Index":"2","Desc":"环境温度较高,虽然不易发生感冒,要提预防空调病"},"Traffic":{"Index":"4","Desc":"有小雨,不太适宜驾驶汽车出行,适当控制车速。"},"Sport":{"Index":"5","Desc":"天气炎热,不适宜户外运动"},"Tour":{"Index":"3","Desc":"天气炎热,比较适宜旅游但要带旱伞或遮阳帽"}}]}

 

------------------------------------------------------------------------

public void testUDecode() {   
    String str = "make in \u4e2d\u56fd";   
    for(char ch : "make in 中国".toCharArray()) {   
        if(ch > 128) {   
            System.out.print("\\u"+Integer.toHexString(ch));   
        } else {   
            System.out.print(ch);   
        }   
    }   
    System.out.println();   
    System.out.println(str);   
    str = "make in \\u4e2d\\u56fd";   
  
    String v = "'"+str+"'";   
    System.out.println(v);   
  
    try {   
        System.out.println(new JSONTokener(v).nextValue().toString());   
    } catch (JSONException e) {   
        e.printStackTrace();   
    }   
}  

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics