有什么方法可以修复 json 响应 [英] Is there any way to fix a json response
问题描述
我正在解析一个有问题的 json.我在 Swift 中这样做:
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
}
但是,我不知道响应的深度.我无权访问我正在调用的 API.那么,有我可以做的解决方案吗?
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?
推荐答案
我有和你一样的需求.我需要以同样的方式传递数组值
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;
}
}
此处为 profileSettings
将此对象传递给 getJSONString
方法,您将获得该格式.
Here for profileSettings
pass this object to getJSONString
method and you will get that format.
这篇关于有什么方法可以修复 json 响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!