解析字符串并将其保存为键值对

时间:2014-08-01 10:49:18

标签: java

我想解析这个字符串,并根据键和值获取值,例如application_id是键,123456是它的值。 请帮助java代码

String testString ="\"application_id\":\"123456\",\"application_name\":\"Shitanshu / Train ticket book\",\"appstore_id\":\"89\",\"appstore_name\":\"Google Play Store\",\"version\":\"1.1\",\"rating\":\"5\",\"title\":\"null\",\"comment\":\"Really fast recharge.... N the repayments are also really fast...\",\"user\":\"narasimham sai\",\"date\":\"2014-07-29\",\"time\":\"20:53:34\",\"iso_code\":\"null\",\"country_name\":\"null\",\"labels\":\"[]\"";                    

1 个答案:

答案 0 :(得分:0)

拆分,上的字符串,拆分:上的对:

Map<String, String> theMap = new HashMap<>();
String[] pairs = testString.split(",");
for (String pair : pairs) {
    String[] parts = pair.replaceAll("\"", "").split(":");
    theMap.put(parts[0], parts[1]);
}

System.out.println(theMap.get("application_id"));

输出:

123456