當前位置:成語大全網 - 新華字典 - ios 請求數據中轉義字符"的處理

ios 請求數據中轉義字符"的處理

前言:根據需求,我將壹個數組類型(每壹個數據都是字典類型)的數據轉化為json字符串的形式傳給後臺。我在需要的地方再請求下來這個數據。結果發現請求下來的數據被轉義處理了,引號被轉義成"。

1.傳給後臺的json字符串。

? NSMutableArray *conArr = [[NSMutableArray alloc] initWithCapacity:0];

for (DynmaticModel*model in self .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];

傳給後臺的priceJson字符串打印如下:

priceJson = [{"type":"1","content":"啦啦啦1"},{"type":"2","content":"-beijing.aliyuncs.com/ios/20200908151508/2020090815150802.png"},{"type":"1","content":"啦啦啦2"}]

2.上傳成功後從後臺請求下來的字符串如下:

[{"type":"1","content":"啦啦啦1"},{"type":"2","content":"-beijing.aliyuncs.com\/ios\/20200908151508\/2020090815150802.png"},{"type":"1","content":"啦啦啦2"}]

3.如何處理:

(1)將字符串中的"用引號替換

?NSString *str = [ text stringByReplacingOccurrencesOfString:@""" withString:@"\""];

這個str就是妳傳上去的json字符串。

(2)將json轉化為數組

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了!