Java 是否有一种方便的方法将逗号分隔的字符串转换为hashmap

Java 是否有一种方便的方法将逗号分隔的字符串转换为hashmap,java,Java,字符串格式不是json格式: a="0PN5J17HBGZHT7JJ3X82", b="frJIUN8DYpKDtOLCwo/yzg=" 我想将此字符串转换为哈希映射: 值为0PN5J17HBGZHT7JJ3X82的键a 具有frJIUN8DYpKDtOLCwo/yzg值的键b= 有没有方便的办法?谢谢 我所尝试的: Map<String, String> map = new HashMap<String, String>(); String s = "

字符串格式不是json格式:

a="0PN5J17HBGZHT7JJ3X82", b="frJIUN8DYpKDtOLCwo/yzg="
我想将此字符串转换为哈希映射:

值为0PN5J17HBGZHT7JJ3X82的键a

具有frJIUN8DYpKDtOLCwo/yzg值的键b=

有没有方便的办法?谢谢

我所尝试的:

    Map<String, String> map = new HashMap<String, String>();
    String s = "a=\"00PN5J17HBGZHT7JJ3X82\",b=\"frJIUN8DYpKDtOLCwo/yzg=\"";
    String []tmp = StringUtils.split(s,',');
    for (String v : tmp) {
        String[] t = StringUtils.split(v,'=');
        map.put(t[0], t[1]);
    }   
我得到这个结果:

值为0PN5J17HBGZHT7JJ3X82的键a

具有frJIUN8DYpKDtOLCwo/yzg值的键b

对于键a,不需要开始和结束双引号;对于键b,起始双引号是不需要的,最后一个等号=丢失。
对不起,我的英语很差。

可能您不在乎它是一个哈希映射,只是一个映射,所以这样就可以了,因为属性实现了映射:

import java.io.StringReader;
import java.util.*;

public class Strings {
    public static void main(String[] args) throws Exception {
        String input = "a=\"0PN5J17HBGZHT7JJ3X82\", b=\"frJIUN8DYpKDtOLCwo/yzg=\"";
        String propertiesFormat = input.replaceAll(",", "\n");
        Properties properties = new Properties();
        properties.load(new StringReader(propertiesFormat));
        System.out.println(properties);
    }
}
输出:

{b="frJIUN8DYpKDtOLCwo/yzg=", a="0PN5J17HBGZHT7JJ3X82"}

如果您确实需要HashMap,可以使用Properties对象作为输入来构造一个HashMap:new HashMapproperties。

根据逗号拆分字符串,然后使用=


在Ryan的代码中添加了一些更改

 public static void main(String[] args) throws Exception {
        String input = "a=\"0PN5J17HBGZHT7JJ3X82\", b=\"frJIUN8DYpKDtOLCwo/yzg=\"";
        input=input.replaceAll("\"", "");
        String propertiesFormat = input.replaceAll(",", "\n");
        Properties properties = new Properties();
        properties.load(new StringReader(propertiesFormat));
        Set<Entry<Object, Object>> entrySet = properties.entrySet();
        HashMap<String,String > map = new HashMap<String, String>();
        for (Iterator<Entry<Object, Object>> it = entrySet.iterator(); it.hasNext();) {
            Entry<Object,Object> entry = it.next();
            map.put((String)entry.getKey(), (String)entry.getValue());
        }
        System.out.println(map);
    }

您也可以使用下面的正则表达式

Map<String,String> data = new HashMap<String,String>();
Pattern p = Pattern.compile("[\\{\\}\\=\\, ]++");
String[] split = p.split(text);
for ( int i=0; i+2 <= split.length; i+=2 ){
    data.put( split[i], split[i+1] );
}
return data;

@AlexErofeev yes,key are String您的输入就像String str=0PN5J17HBGZHT7JJ3X82,frJIUN8DYpKDtOLCwo/yzg;或者字符串str=a=\0PN5J17HBGZHT7JJ3X82\,b=\frJIUN8DYpKDtOLCwo/yzg=\?很抱歉我的问题不清楚。我不想要值中的引号。我想要结果:{b=frJIUN8DYpKDtOLCwo/yzg=,a=0PN5J17HBGZHT7JJ3X82}您应该从该链接中取出相关的正则表达式,并将其包含在您的答案中。那样的话,如果链接一旦失效,答案就会被保留下来。
Map<String,String> data = new HashMap<String,String>();
Pattern p = Pattern.compile("[\\{\\}\\=\\, ]++");
String[] split = p.split(text);
for ( int i=0; i+2 <= split.length; i+=2 ){
    data.put( split[i], split[i+1] );
}
return data;