调用JSONSerialization.JSONObjectWithData时存在多余参数,该行抛出Error,如何解决?
JSONSerialization.jsonObject(with:options:) Hey there! Let’s work through this issue with JSONSerialization together—this is a super common pitfall when adjusting to Swift’s error handling or moving between language versions.
First: Fix the "Extra Argument" Warning/Error
That extra parameter alert almost always comes from using an outdated method signature. Here’s the breakdown:
- If you’re copying Objective-C-style code or using older Swift syntax, you might have a call like
JSONSerialization.JSONObjectWithData(data, options: .allowFragments, error: &error). - Modern Swift (3+) reworked this method to use throwable error handling instead of passing an
NSErrorpointer. Theerrorparameter is no longer needed, so including it counts as an extra argument.
Correct the Method Call
Replace your old call with a Swift-native approach:
- Remove the
error: &someErrorparameter entirely. - Use
trywith either ado-catchblock (for explicit error handling) ortry?(if you just want an optional result and can ignore errors temporarily).
Second: Troubleshoot the Thrown Error
Once you fix the parameter issue, if you’re still getting an error thrown, let’s dig into the root causes:
1. Validate Your Input Data
First, confirm the Data you’re passing is actually valid JSON. Print its raw string representation to spot syntax issues:
if let jsonString = String(data: yourData, encoding: .utf8) { print("Raw JSON string: \(jsonString)") }
Look for common mistakes like unclosed quotes, missing commas between key-value pairs, or mismatched braces/brackets.
2. Check Your options Parameter
The options flag should match your JSON structure:
- Use
.allowFragmentsonly if your JSON is a single value (like a string, number, or boolean) instead of a top-level object or array. If your JSON starts with{or[, skip this option. - For most cases, passing an empty array
[]works perfectly fine.
3. Capture Exact Error Details
Don’t just swallow the error—use a do-catch block to get specific info about what’s failing:
do { let json = try JSONSerialization.jsonObject(with: yourData, options: []) // Successfully parsed! Now cast to your expected type (e.g., [String: Any]) if let jsonDict = json as? [String: Any] { // Work with your dictionary } } catch let error as NSError { print("JSON Parsing Failed:") print("Message: \(error.localizedDescription)") print("Error Code: \(error.code)") // Common codes: 3840 = syntax error print("Details: \(error.userInfo)") }
This will tell you exactly if it’s a syntax error, invalid data encoding, or something else.
Full Working Example
Here’s a complete snippet to reference:
// Sample valid JSON data let sampleJsonData = """ { "username": "talha_shafaqat", "isActive": true, "posts": [1, 3, 5] } """.data(using: .utf8)! do { guard let parsedJson = try JSONSerialization.jsonObject(with: sampleJsonData, options: []) as? [String: Any] else { print("Parsed JSON isn't a dictionary as expected") return } print("Success! Parsed JSON: \(parsedJson)") } catch { print("Failed to parse JSON: \(error.localizedDescription)") }
内容的提问来源于stack exchange,提问作者Talha Shafaqat

