JSON 创建于 objective c,无效 JSON
JSON creation in objective c, invalid JSON
我正在创建一个 JSON
以在线检查其显示错误时发送到服务中:
Error: Parse error on line 1:
[{\ "SAHExpertCode\"
--^ Expecting 'STRING', '}', got 'undefined'
My JSON is [{\"SAHExpertCode\" : \"\", \"ShiftType\" : \"AM\", \"LocFunId\" : \"CLT0004218\", \"SAHQualCode\" : \"CA\" }]
Please tell me what's wrong and how to correct it.First i am making JSON filterString = [{ "SAHExpertCode" : "", "ShiftType" : "AM", "LocFunId" : "CLT0004218","SAHQualCode" : "CA" }] on checking found it is correct then creating a dictionary NSDictionary*dictData=@{@"MbrId":[USER_DEFAULTS valueForKey:@"MemberId"],@"StrFilter":[NSString stringWithFormat:@"%@",filterString],@"shiftCrtlNos":shftCntrlNmbrs}; NSMutableArray *finalArray = [[NSMutableArray alloc]init]; [finalArray addObject:dictData]; NSString *finalString =[self ConvertArrayToJsonData:finalArray];finalString = [finalString stringByReplacingOccurrencesOfString:@"\n" withString:@""]; finalString = [finalString stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];after creation of final string it is generating symbol \ for JSON Conversion my code is -(NSString *)ConvertArrayToJsonData:(NSMutableArray *)array{ NSError error;
NSData jsonData = [NSJSONSerialization dataWithJSONObject:array options:NSJSONWritingPrettyPrinted error:&error]; NSString *JSONString; if (!jsonData) { NSLog(@"error :%@",error); } else {JSONString = [[NSString alloc] initWithBytes:[jsonData bytes] length:[jsonData length] encoding:NSUTF8StringEncoding]; // NSLog(@"jsonstring:%@",JSONString);
}
return JSONString;
}
//i need JSON like [ { "StrFilter" : "[ { \"SAHExpertCode\" : \"\", \"ShiftType\" : \"PM\", \"LocFunId\" : \"CLT0004218\", \"SAHQualCode\" : \"CA\" }]", "MbrId" : "MBR0000035", "shiftCrtlNos" : "0080013526,0080014697" }] also tell me how to remove \ from String
我通过执行替换了不需要的 \ 符号
finalString = [finalString stringByReplacingOccurrencesOfString:@"\" withString:@""];在 JSON 字符串中。
我建议您转到 www.json.org 并查看 JSON 数据的正确格式。获得正确格式 JSON 的最简单方法是创建一个要转换为 JSON 的数组或字典,然后使用 NSJSONSerialization 来完成这项工作。
直接使用iOS
的默认NSJSON序列化方法
在下面的示例中,我有一个 "postMenuArray",我正在将其转换为 JSON。
NSData * postMenuSerial = [NSJSONSerialization dataWithJSONObject:postMenuArray options:0 error:nil];
NSString *Menujson = [[NSString alloc] initWithBytes:[postMenuSerial bytes] length:[postMenuSerial length] encoding:NSUTF8StringEncoding];
这里我得到了 JSON 回复
Menujson = [Menujson stringByReplacingOccurrencesOfString:@" "withString:@""];
最后我删除了所有间距。
仍然面临问题试试这个
Menujson = [Menujson stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
我正在创建一个 JSON
以在线检查其显示错误时发送到服务中:
Error: Parse error on line 1: [{\ "SAHExpertCode\" --^ Expecting 'STRING', '}', got 'undefined' My JSON is [{\"SAHExpertCode\" : \"\", \"ShiftType\" : \"AM\", \"LocFunId\" : \"CLT0004218\", \"SAHQualCode\" : \"CA\" }] Please tell me what's wrong and how to correct it.First i am making JSON filterString = [{ "SAHExpertCode" : "", "ShiftType" : "AM", "LocFunId" : "CLT0004218","SAHQualCode" : "CA" }] on checking found it is correct then creating a dictionary NSDictionary*dictData=@{@"MbrId":[USER_DEFAULTS valueForKey:@"MemberId"],@"StrFilter":[NSString stringWithFormat:@"%@",filterString],@"shiftCrtlNos":shftCntrlNmbrs}; NSMutableArray *finalArray = [[NSMutableArray alloc]init]; [finalArray addObject:dictData]; NSString *finalString =[self ConvertArrayToJsonData:finalArray];finalString = [finalString stringByReplacingOccurrencesOfString:@"\n" withString:@""]; finalString = [finalString stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];after creation of final string it is generating symbol \ for JSON Conversion my code is -(NSString *)ConvertArrayToJsonData:(NSMutableArray *)array{ NSError error; NSData jsonData = [NSJSONSerialization dataWithJSONObject:array options:NSJSONWritingPrettyPrinted error:&error]; NSString *JSONString; if (!jsonData) { NSLog(@"error :%@",error); } else {JSONString = [[NSString alloc] initWithBytes:[jsonData bytes] length:[jsonData length] encoding:NSUTF8StringEncoding]; // NSLog(@"jsonstring:%@",JSONString); } return JSONString; } //i need JSON like [ { "StrFilter" : "[ { \"SAHExpertCode\" : \"\", \"ShiftType\" : \"PM\", \"LocFunId\" : \"CLT0004218\", \"SAHQualCode\" : \"CA\" }]", "MbrId" : "MBR0000035", "shiftCrtlNos" : "0080013526,0080014697" }] also tell me how to remove \ from String
我通过执行替换了不需要的 \ 符号 finalString = [finalString stringByReplacingOccurrencesOfString:@"\" withString:@""];在 JSON 字符串中。
我建议您转到 www.json.org 并查看 JSON 数据的正确格式。获得正确格式 JSON 的最简单方法是创建一个要转换为 JSON 的数组或字典,然后使用 NSJSONSerialization 来完成这项工作。
直接使用iOS
的默认NSJSON序列化方法在下面的示例中,我有一个 "postMenuArray",我正在将其转换为 JSON。
NSData * postMenuSerial = [NSJSONSerialization dataWithJSONObject:postMenuArray options:0 error:nil];
NSString *Menujson = [[NSString alloc] initWithBytes:[postMenuSerial bytes] length:[postMenuSerial length] encoding:NSUTF8StringEncoding];
这里我得到了 JSON 回复
Menujson = [Menujson stringByReplacingOccurrencesOfString:@" "withString:@""];
最后我删除了所有间距。
仍然面临问题试试这个
Menujson = [Menujson stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];