mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-12 07:20:45 +00:00
17 lines
646 B
Swift
17 lines
646 B
Swift
import Foundation
|
|
|
|
enum JSONObjectExtractionSupport {
|
|
static func extract(from raw: String) -> (text: String, object: [String: Any])? {
|
|
let trimmed = raw.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
guard let start = trimmed.firstIndex(of: "{"),
|
|
let end = trimmed.lastIndex(of: "}")
|
|
else {
|
|
return nil
|
|
}
|
|
let jsonText = String(trimmed[start...end])
|
|
guard let data = jsonText.data(using: .utf8) else { return nil }
|
|
guard let object = try? JSONSerialization.jsonObject(with: data) as? [String: Any] else { return nil }
|
|
return (jsonText, object)
|
|
}
|
|
}
|