I have Java string('s) in the format below:
String s = "[
"samsung",
["samsung galaxy s9 case","samsung galaxy s8 case","samsung galaxy s9 plus case","samsung galaxy s8 charger"],
[{"nodes":[{"name":"Cell Phones & Accessories","alias":"mobile"}]},{},{},{},{},{},{},{},{},{}],
[],
"1XQ3CN8WM8VSE"
]"
What's the best way to process the String so I can get these values (of 2nd item enclosed with [])
"samsung galaxy s9 case","samsung galaxy s8 case","samsung galaxy s9 plus case","samsung galaxy s8 charger"
inside a List<String>?
Update
The String is valid JSON and tested with the code
public static boolean isJSONValid(String test) {
try {
new JSONObject(test);
} catch (JSONException ex) {
// edited, to include @Arthur's comment
// e.g. in case JSONArray is valid as well...
try {
new JSONArray(test);
} catch (JSONException ex1) {
return false;
}
}
return true;
}
I also tried to parse it as JSON (as suggested) but I get exception provided.
JSONObject obj = new JSONObject(s);
Exception in thread "main" org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1] .
My String always starts with [..]
[), not a JSON Object (starts with{). Usenew JSONArray(test)instead. Please learn JSON if you're going to work with it. See json.org