Java String to Map stringToMap(String value)

Here you can find the source of stringToMap(String value)

Description

string To Map

License

Open Source License

Declaration

public static Map stringToMap(String value) throws Exception 

Method Source Code

//package com.java2s;

import java.util.HashMap;

import java.util.Map;

public class Main {
    public static Map stringToMap(String value) throws Exception {
        if (value == null || value.length() == 0)
            throw new Exception();

        if (value.startsWith("{") || value.startsWith("["))
            value = value.substring(1);//from w ww.j  ava 2 s.c  o m

        if (value.endsWith("}") || value.endsWith("]"))
            value = value.substring(0, value.length() - 1);

        String[] elements = value.split(",");
        Map<String, String> map = new HashMap();
        for (int index = 0; index < elements.length; index++) {
            String[] pair = elements[index].split(":");
            map.put(pair[0], pair[1]);
        }
        return map;
    }
}

Related

  1. stringToMap(String source, String delimiter)
  2. stringToMap(String src)
  3. stringToMap(String src)
  4. StringToMap(String str)
  5. stringToMap(String str)
  6. stringToMap(String[] strings)
  7. toMap(String componentName, String contextName)
  8. toMap(String jsonStr)
  9. toMap(String parameters)