前言:根據(jù)需求,我將一個(gè)數(shù)組類型(每一個(gè)數(shù)據(jù)都是字典類型)的數(shù)據(jù)轉(zhuǎn)化為json字符串的形式傳給后臺(tái)。我在需要的地方再請(qǐng)求下來(lái)這個(gè)數(shù)據(jù)。結(jié)果發(fā)現(xiàn)請(qǐng)求下來(lái)的數(shù)據(jù)被轉(zhuǎn)義處理了,引號(hào)被轉(zhuǎn)義成"。
1.傳給后臺(tái)的json字符串。
? NSMutableArray *conArr = [[NSMutableArray alloc] initWithCapacity:0];
?? ? for(DynmaticModel*modelinself.dataArray)
?? ? {
?? ? ? ? if(model.content&& model.content.length>0) {
?? ? ? ? ? ? NSDictionary*dic =@{@"type":model.type,@"content":model.content};
?? ? ? ? ? ? [conArraddObject:dic];
?? ? ? ? }
?? ? }
? ? NSData *priceDta = [NSJSONSerialization dataWithJSONObject:conArr options:kNilOptions error:nil];
? ? NSString *priceJson = [[NSString alloc] initWithData:priceDta? encoding:NSUTF8StringEncoding];
傳給后臺(tái)的priceJson字符串打印如下:
priceJson = [{"type":"1","content":"啦啦啦1"},{"type":"2","content":"http://fengwojiancai.oss-cn-beijing.aliyuncs.com/ios/20200908151508/2020090815150802.png"},{"type":"1","content":"啦啦啦2"}]
2.上傳成功后從后臺(tái)請(qǐng)求下來(lái)的字符串如下:
[{"type":"1","content":"啦啦啦1"},{"type":"2","content":"http:\/\/fengwojiancai.oss-cn-beijing.aliyuncs.com\/ios\/20200908151508\/2020090815150802.png"},{"type":"1","content":"啦啦啦2"}]
3.如何處理:
(1)將字符串中的"用引號(hào)替換
?NSString *str = [ text stringByReplacingOccurrencesOfString:@""" withString:@"\""];
這個(gè)str就是你傳上去的json字符串。
(2)將json轉(zhuǎn)化為數(shù)組
NSData *JSONData = [str dataUsingEncoding:NSUTF8StringEncoding];
? ? ? ? NSError*error =nil;
? ? ? ? NSArray * arr = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingAllowFragments error:&error];
? ? ? ? for(NSDictionary *dic??in??arr) {
? ? ? ? ? ? NSLog(@"dic = %@",dic);
? ? ? ? }
這樣就ok了!