Is there any way to fix a json response

2019-09-17 21:39发布

问题:

I am parsing a json which is faulty. I am doing this in Swift like this:

jsonString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)

So, it gives an error.

Error converting string to object => Optional(Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Badly formed object around character 11396.) UserInfo=0x14512990 {NSDebugDescription=Badly formed object around character 11396.})

Now, the response is a huge one, so I am posting only the error part of it:

{
  "data" : {
    "operator_settings" : {
      "profileSettings" : "{"visible":["firstname","lastname","phonenumber","emailaddress","paddress","paddress2","paddresscity","paddressstateabbreviation","paddresszip","paddresscountry"],"required":["firstname","lastname","emailaddress","paddress","paddresscity","paddresszip"]}"
    }
  },
  "status" : 200.0,
  "count" : null
}

Also, I want to make this response like this before parsing it:

{
"data": {
    "operator_settings": {
        "profileSettings": "{\"visible\":[\"firstname\",\"lastname\",\"phonenumber\",\"emailaddress\",\"paddress\",\"paddress2\",\"paddresscity\",\"paddressstateabbreviation\",\"paddresszip\",\"paddresscountry\"],\"required\":[\"firstname\",\"lastname\",\"emailaddress\",\"paddress\",\"paddresscity\",\"paddresszip\"]}"
    }
},
"status": 200,
"count": null

}

But, I do not know the depth of the response. I do not have access to the API which I am calling. So, is there a solution that I can do?

回答1:

I have requirement as like yours. Same way i need to pass array values with the format

 "{\n  \"UserId\" : \"1\",\n  \"CorporateId\" : \"1\",\n  \"DataAccessLevel\" : \"1\"\n}"

So for that I have created two methods below:

+ (NSString *) getJSONString:(id)object {

    NSString *jsonString = @"";
    @try {
        NSError *error = nil;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:object
                                                           options:NSJSONWritingPrettyPrinted
                                                             error:&error];
        if (! jsonData) {
            NSLog(@"Got an error: %@", error);
        } else {
            jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        }
        return jsonString;
    }
    @catch (NSException *exception) {
        NSLog(@" Exception :%@",exception);
        return jsonString;
    }
}

//---------------------------------------------------------------

+ (id) getObjectFromJSONString:(NSString *)jsonString {
    @try {
        NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
        NSError *error = nil;
        id object = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
        return object;
    }
    @catch (NSException *exception) {
        NSLog(@" Exception :%@",exception);
        return nil;
    }
}

Here for profileSettings pass this object to getJSONString method and you will get that format.