如何从 Google 拆分器返回的集合中读取值?
How to read values from collections returned by Google splitter?
MvcResult result;
result = this.mockMvc.perform(something).andExpect( status().isOk() ).andReturn();
String resultAsString = result.getResponse().getContentAsString();
/* resultAsString = "{"abc":"def","ghi":"jkl","mno":"pqr"}" */
String resultAsString1 = StringUtils.remove( resultAsString, "{" );
resultAsString1 = StringUtils.remove( resultAsString1, "}" );
Map<String, String> resultAsMap = Splitter.on( "," ).withKeyValueSeparator( ":" ).split( resultAsString1 );
String myValueName = (String) resultAsMap.get( "mno" );
但是在调试模式下,我看到的是 myValueName = null。
有人可以帮忙吗?
我正在导入 com.google.common.base.Splitter;
输入字符串中的键用引号括起来,但用于地图查找的键没有。您可能想改用 JSON 库,例如 Gson:
JsonObject obj = (JsonObject) (new JsonParser().parse("{\"key\": \"value\"}"));
String value = obj.get("key");
MvcResult result;
result = this.mockMvc.perform(something).andExpect( status().isOk() ).andReturn();
String resultAsString = result.getResponse().getContentAsString();
/* resultAsString = "{"abc":"def","ghi":"jkl","mno":"pqr"}" */
String resultAsString1 = StringUtils.remove( resultAsString, "{" );
resultAsString1 = StringUtils.remove( resultAsString1, "}" );
Map<String, String> resultAsMap = Splitter.on( "," ).withKeyValueSeparator( ":" ).split( resultAsString1 );
String myValueName = (String) resultAsMap.get( "mno" );
但是在调试模式下,我看到的是 myValueName = null。
有人可以帮忙吗?
我正在导入 com.google.common.base.Splitter;
输入字符串中的键用引号括起来,但用于地图查找的键没有。您可能想改用 JSON 库,例如 Gson:
JsonObject obj = (JsonObject) (new JsonParser().parse("{\"key\": \"value\"}"));
String value = obj.get("key");