Reputation: 5989
I have this values in cellHashMap
key = name
value = {name ={uri=/book/bookItem3(852)/header}}
I want to get only the
book/bookItem3(852)/header
( without the {uri= and the } in the end _
I did this code :
Map<String, Object> cellHashMap = (HashMap<String, Object>) cell.getValue();
String cellValue = cellHashMap.get("name").toString(); //$NON-NLS-1$
String[] splitCell = cellValue.split("uri=/"); //$NON-NLS-1$
return splitCell[1].substring(0, splitCell[1].length() - 1);
The result of the split are :
splitCell[0] = {
splitCell[1] = sbook/bookItem3(852)/header}
it is working but I think it is not the best way to do it
Do you know on better way?
Upvotes: 0
Views: 87
Reputation: 8617
My guess is you are not looking to parse JSON since as pointed out, your String isn't a JSON after all. You can however parse this String through normal String manipulation, sample below:
final static String str = "{name ={uri=/book/bookItem3(852)/header}}";
public static void main (String[] args) {
String value = str.substring((str.indexOf("uri=/") + "uri=/".length()), str.indexOf("}"));
System.out.println(value);
}
Output:
book/bookItem3(852)/header
Upvotes: 2
Reputation: 2279
I'm not sure if you have the ability to actually store JSON values in your dictionary, but if you can correct the formatting to actual JSON you can use the JSONObject class to parse the JSON string & retrieve the data.
String jsonstring = "The valid JSON string in your hashmap"
JSONObject json = new JSONObject(jsonstring)
String path = (String) json.get("name");
Again, this method will only work if your hashmap stores valid JSON.
Upvotes: 2
Reputation: 17945
This is not JSON (see comments above). If you want to parse what you appear to have (as opposed to what you say you have), you can use the following:
int start = myString.indexOf("uri=");
if (start == -1) {
// handle "not found"
}
int end = myString.indexOf("}", start + "uri=".length());
if (end == -1) {
// handle "not found"
}
String goal = myString.substring(start + "uri=".length(), end);
// goal = "book/bookItem3(852)/header"
Or a regex:
Matcher m = Pattern.compile("uri=([^}]+)").matcher(myString);
if (m.find()) {
goal = m.group(1); // "book/bookItem3(852)/header"
} else {
// handle not found
}
Upvotes: 2