JSONSerialization Invalid type in JSON write (_SwiftValue)

前端 未结 10 918
情书的邮戳
情书的邮戳 2020-12-14 14:14

Why does the following code give me the error:

Invalid type in JSON write (_SwiftValue).

The error is thrown on this line:

相关标签:
10条回答
  • 2020-12-14 14:23

    If your problem is still not resolved by the answer given here. I believe one of your objects inside the parameters might not be an instance of NSString, NSNumber, NSArray, NSDictionary, or NSNull. As given in the documentation for JSONSerialization class:

    An object that may be converted to JSON must have the following properties:

    1. The top level object is an NSArray or NSDictionary. All objects are instances of NSString, NSNumber, NSArray, NSDictionary, or NSNull.

    2. All dictionary keys are instances of NSString. Numbers are not NaN or infinity.

    3. Other rules may apply. Calling isValidJSONObject(_:) or attempting a conversion are the definitive ways to tell if a given object can be converted to JSON data.

    So, please check if any of the objects in your parameters object doesn't satisfy the above constraints.

    0 讨论(0)
  • 2020-12-14 14:25

    I had this problem and it was because one of my strings was Optional. It was trying to serialize a value like: "Optional(\"string value\")"

    Instead of "string value"

    0 讨论(0)
  • 2020-12-14 14:26

    If you're using SwiftyJSON to access a JSON object, it's important to use the dictionaryObject property of the JSON (instead of using dictionaryValue, dictionary or nothing at all), because you will get this error (or a variation of it) otherwise. For example:

    guard let jsonDict = json.dictionaryObject else {
        return
    }
    
    let jsonData = try JSONSerialization.data(withJSONObject: jsonDict, options: [])
    
    0 讨论(0)
  • 2020-12-14 14:29

    You can call this method too and see if your parameter can be converted to a JSON object, this will return a Bool.

    let checker = JSONSerialization.isValidJSONObject(parameters)
    
    0 讨论(0)
  • 2020-12-14 14:32

    You should convert NSObject to NSDictionary at first

    Try this to convert to NSDictionary.

    #import <objc/runtime.h>
    
    //Add this utility method in your class.
    + (NSDictionary *)dictionaryWithPropertiesOfObject:(id)obj {
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    
        unsigned count;
        objc_property_t *properties = class_copyPropertyList([obj class], &count);
    
        for (int i = 0; i < count; i++) {
            NSString *key = [NSString stringWithUTF8String:property_getName(properties[i])];
            [dict setObject:[obj valueForKey:key] ? [obj valueForKey:key] : @"" forKey:key];
        }
    
        free(properties);
    
        return [NSDictionary dictionaryWithDictionary:dict];
    }
    

    Then call this:

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:0 error:&err];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
    0 讨论(0)
  • 2020-12-14 14:34

    In my case I accidentally added an object to the Parameters dictionary instead of a string

    0 讨论(0)
提交回复
热议问题