Parse a URI String into Name-Value Collection

前端 未结 19 2500
难免孤独
难免孤独 2020-11-22 01:34

I\'ve got the URI like this:

https://google.com.ua/oauth/authorize?client_id=SS&response_type=code&scope=N_FULL&access_type=offline&redirect_         


        
19条回答
  •  死守一世寂寞
    2020-11-22 02:09

    use google Guava and do it in 2 lines:

    import java.util.Map;
    import com.google.common.base.Splitter;
    
    public class Parser {
        public static void main(String... args) {
            String uri = "https://google.com.ua/oauth/authorize?client_id=SS&response_type=code&scope=N_FULL&access_type=offline&redirect_uri=http://localhost/Callback";
            String query = uri.split("\\?")[1];
            final Map map = Splitter.on('&').trimResults().withKeyValueSeparator('=').split(query);
            System.out.println(map);
        }
    }
    

    which gives you

    {client_id=SS, response_type=code, scope=N_FULL, access_type=offline, redirect_uri=http://localhost/Callback}
    

提交回复
热议问题