将每行的名称/值对保存到HashMap

时间:2016-05-11 18:45:01

标签: java hashmap

我有以下模式从文件中获取:

....Name1:Value1.....Name2:Value2....Name3:Value3.....
....Name4:Value4.....Name5:Value5....Name6:Value6.....

我有多行如下(myJSONString)构成我正在阅读的文件。

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class removeMe { 

    public static void main(String[] args) {

        String myJSONString = "{\"message\":{\"method\":\"Collect\",\"params\":{\"args\":{\"data\":{\"columnNumber\":1,\"lineNumber\":1,\"url\":\"http://localhost/MyScript.php\"},\"fileName\":\"http://localhost/MyScript.js\"},\"cPat\":\"tools\",\"dur\":182,\"name\":\"copile\",\"ph\":\"X\",\"pid\":11744,\"tdur\":145,\"tid\":12488,\"ts\":369069849845.0,\"tts\":299168}}}";

        String patternString = "(fileName\":\"|dur\":|tdur\":)([^\",]*)";

        List<Map<String,String>> mapList = new ArrayList<Map<String,String>>();

        Map<String,String> map1 = new HashMap<String,String>();

        Collections.addAll(mapList, map1, map2,....);

        Matcher matcher = Pattern.compile(patternString).matcher(myJSONString);

        while (matcher.find()) {
            map1.put(matcher.group(1).replaceAll(("\".*"),""),matcher.group(2));
        }
    }
}

每次我读取并从行中获取值时,我想创建一个类似map1的地图。然后将其添加到数组列表中。

0 个答案:

没有答案