gautam
gautam

Reputation: 197

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" );

But in debug mode, what I'm seeing is that myValueName = null.

Can someone please help?

I'm importing com.google.common.base.Splitter;

Upvotes: 0

Views: 80

Answers (1)

jspcal
jspcal

Reputation: 51914

The keys in the input string are enclosed in quotes, but the key used for the map lookup isn't. You may want to use a JSON library instead, like Gson:

JsonObject obj = (JsonObject) (new JsonParser().parse("{\"key\": \"value\"}"));
String value = obj.get("key");

Upvotes: 1

Related Questions