1

I have data as follows:

String s = "foo.com^null^[]"; 
String s1 = "bar.com^null^[{\"seen_first\":1357882827,\"seen_last\":1357882827,\"_id\":\"93.170.52.31\",\"exclude_from_publication\":false,\"locked\":false,\"agent\":\"domain_export\",\"web_published\":true,\"version\":\"IPv4\"},{\"seen_first\":1357882827,\"seen_last\":1357882827,\"_id\":\"93.170.52.21\",\"exclude_from_publication\":false,\"locked\":false,\"agent\":\"domain_export\",\"web_published\":true,\"version\":\"IPv4\"}]";

And note that third field.. it can be either [] or a json array. And I am trying to parse these fields..

Here is my current attempt.

public static void check(String s) {
        String [] tokens = s.split("^");
        System.out.println(tokens[0]);
        System.out.println(tokens[1]);
        System.out.println(tokens[2]);


        if (tokens[2].trim().equals("[]")) {
            System.out.println("here--> " +true);

        }
        System.out.println("---------");


    }

What am i doing wrong?

4

1 回答 1

2

^是正则表达式中的元字符,意思是“字符串的开头”。你需要逃避它:

String [] tokens = s.split("\\^");
于 2013-10-09T20:08:17.563 回答