I know using myString.split(",");
would work if I just had a string separated by commas like "a", "b", "c"
, but what if I have a more complicated string like:
["Meal End", "meal-end", "menu", {"color":"black", "width":1}]
I want to separate out the elements
"Meal End"
"meal-end"
"menu"
{"color":"black", "width":1}
My first approach was to get a substring of that whole thing from (1 to string.length - 1). That removed the [ ]. Then I used string.split(",") which almost worked... but for element 3 I end up with {"color":"black" which I do not want.
I want the whole curly brackets object. How can I do this, other than manually searching through the whole string, stopping when I find a {, and continuing when I get to }?
I considered using JSONObject but my string is enclosed in [ ] ... I could always replace those with { } but then there's no key still...
I would suggest "jackson". It parses this string as a list containing 3 strings and 1 map.
List<?> value = (List<?>) new ObjectMapper().readValue(
"[\"Meal End\", \"meal-end\", \"menu\", {\"color\":\"black\", \"width\":1}]",
Object.class);
System.out.println(value.get(0));
Map<?, ?> map = (Map<?, ?>) value.get(3);
System.out.println(map.get("color"));
Output:
Meal End
black
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.