Simple JSON string array extraction - handles nested brackets in strings
| 256 | |
| 257 | // Simple JSON string array extraction - handles nested brackets in strings |
| 258 | static std::string extractJsonStringArray(const std::string& json, const std::string& key) |
| 259 | { |
| 260 | std::string needle = "\"" + key + "\""; |
| 261 | size_t pos = json.find(needle); |
| 262 | if (pos == std::string::npos) |
| 263 | return ""; |
| 264 | pos = json.find('[', pos); |
| 265 | if (pos == std::string::npos) |
| 266 | return ""; |
| 267 | // Find matching ] accounting for strings |
| 268 | int depth = 0; |
| 269 | bool inStr = false; |
| 270 | for (size_t i = pos; i < json.size(); i++) |
| 271 | { |
| 272 | if (inStr) |
| 273 | { |
| 274 | if (json[i] == '\\') |
| 275 | { |
| 276 | i++; |
| 277 | continue; |
| 278 | } |
| 279 | if (json[i] == '"') |
| 280 | inStr = false; |
| 281 | continue; |
| 282 | } |
| 283 | if (json[i] == '"') |
| 284 | { |
| 285 | inStr = true; |
| 286 | continue; |
| 287 | } |
| 288 | if (json[i] == '[') |
| 289 | depth++; |
| 290 | if (json[i] == ']') |
| 291 | { |
| 292 | depth--; |
| 293 | if (depth == 0) |
| 294 | return json.substr(pos, i - pos + 1); |
| 295 | } |
| 296 | } |
| 297 | return ""; |
| 298 | } |
| 299 | |
| 300 | static std::vector<std::string> parseStringArray(const std::string& arr) |
| 301 | { |