2013.10.8 pinyin4j.jar gson.jar

1, pinyin4j.jar 可以把汉字转为拼音

	
    public static String getPinYin(String zhongwen)   
            throws BadHanyuPinyinOutputFormatCombination {   
  
        String zhongWenPinYin = "";   
        char[] chars = zhongwen.toCharArray();   
  
        for (int i = 0; i < chars.length; i++) {   
            String[] pinYin = PinyinHelper.toHanyuPinyinStringArray(chars[i], getDefaultOutputFormat());   
            if (pinYin != null) {   
            	zhongWenPinYin += pinYin[0];   
            } else {   
                zhongWenPinYin += chars[i];   
            }   
        }   
        return zhongWenPinYin;   
    }
    
    private static HanyuPinyinOutputFormat getDefaultOutputFormat() {   
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();   
//        format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_U_AND_COLON);
        return format;   
    } 

2,gson.jar 是google提供的一个解释json的框架。功能上和fastjson类似。.

		Score s = new Score();
		s.setS(20);
		s.setSubject("eng");
		
		Score s1 = new Score();
		s1.setS(30);
		s1.setSubject("math");
		Score s2 = new Score();
		s2.setS(50);
		s2.setSubject("math2");
		
		List<Score> list = new ArrayList<>();
		
		list.add(s1);
		list.add(s2);
		
		s.setList(list);
		
		Gson gson = new Gson();
		String json = gson.toJson(s,Score.class);
		System.out.println(json);
		
		Score ss = gson.fromJson(json, Score.class);
		System.out.println(ss.getList().get(0).getS());

	static class Score {
		String subject;
		int s;
		List<Score> list;
		public List<Score> getList() {
			return list;
		}
		public void setList(List<Score> list) {
			this.list = list;
		}
		public void setSubject(String subject) {
			this.subject = subject;
		}
		public String getSubject() {
			return subject;
		}
		public int getS() {
			return s;
		}
		public void setS(int s) {
			this.s = s;
		}
		
	}

注意:gson不容许“循环引用”,比如在上例中list.add(s),否则会报错:circular reference error



你可能感兴趣的:(2013.10.8 pinyin4j.jar gson.jar)