{"A0":"2011-10-11 12:08:06:This is the first message","A1":"2011-10-11 12:08:06:This is the second message"}
我只要得到2011-10-11 12:08:06这样的时间和后面This is the first message消息就可以了,前面可能是A0到A1000。最开始我想用splite按逗号分出A0和A1的消息内容后再通过“:”符分割出时间和消息,但发现时间里面有“:”符所以作罢。请问大家有相应的解决经验么?
我只要得到2011-10-11 12:08:06这样的时间和后面This is the first message消息就可以了,前面可能是A0到A1000。最开始我想用splite按逗号分出A0和A1的消息内容后再通过“:”符分割出时间和消息,但发现时间里面有“:”符所以作罢。请问大家有相应的解决经验么?
public static void main(String[] args) {
String testStr = "{\"A0\":\"2011-10-11 12:08:06:This is the first message\",\"A1\":\"2011-10-11 12:08:06:This is the second message\"}";
String reg = "\"A[\\d]+\":\"([\\d]{4}-[\\d]{2}-[\\d]{2} [\\d]{2}:[\\d]{2}:[\\d]{2}):([^\"]+)\"";
Pattern p = Pattern.compile(reg);
Matcher matcher = p.matcher(testStr);
while (matcher.find()) {
System.out.println(matcher.group(1) + " " + matcher.group(2));
}
}
<script>
var a = '{"A0":"2011-10-11 12:08:06:This is the first message","A1":"2011-10-11 12:08:06:This is the second message"}';
var obj = eval("(" + a + ")")
alert(obj.A0.split(":")[3])
alert(obj.A1.split(":")[3]) </script>你的字符串是如何产生的?
你要保证字符串的格式,我还是觉得这种方法不保险。
是服务器发过来的xml中的内容,我把它解析出来了,就是这样的字符串。