Java正则解析JSON数据

package test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {
	public static void main(String[] args) {
		String s = "[{\"name\":\"zhangsan\",\"sex\":\"男\"},{\"name\":\"lisi\",\"sex\":\"女\"},{\"name\":\"wangwu\",\"sex\":\"男\"}]";
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		Map<String, String> map = null;
		Pattern p = Pattern.compile("(\"\\w+\"):(\"[^\"]+\")");
		Matcher m = p.matcher(s);
		String[] strs = null;
		while (m.find()) {
			strs = m.group().split(":");
			if (strs.length == 2) {
				map = new HashMap<String, String>();
//				System.out.println(_strs[0].replaceAll("\"", "").trim() + "="
//						+ _strs[1].trim().replaceAll("\"", ""));
				map.put(strs[0].replaceAll("\"", "").trim(), strs[1].trim()
						.replaceAll("\"", ""));
				list.add(map);
			}

		}
		 System.out.println(list);
	}
}

你可能感兴趣的:(java,json)