Objective c NSJSONSerialization中的未替换控制字符

Objective c NSJSONSerialization中的未替换控制字符,objective-c,escaping,nsjsonserialization,Objective C,Escaping,Nsjsonserialization,我需要解析这个JSON。当我使用NSJSONSerialization执行此操作时,会出现“字符1981周围的未替换控制字符”错误 我需要知道: 什么是该死的未转义的控制字符?有清单吗 我如何消除这个错误?最简单的方法 提前感谢。在使用NSJSONSerialization之前,您在做什么?检查编码,也许问题就在那里 我很快试了一下,结果成功了。源JSON是有效的。这就是我用漂亮的打印工具将数组序列化回JSON时得到的结果: [ { "url2": "http://ielts.p

我需要解析这个JSON。当我使用NSJSONSerialization执行此操作时,会出现“字符1981周围的未替换控制字符”错误

我需要知道:

  • 什么是该死的未转义的控制字符?有清单吗
  • 我如何消除这个错误?最简单的方法

提前感谢。

在使用NSJSONSerialization之前,您在做什么?检查编码,也许问题就在那里

我很快试了一下,结果成功了。源JSON是有效的。这就是我用漂亮的打印工具将数组序列化回JSON时得到的结果:

[
  {
    "url2": "http://ielts.progmic.com/app/i_COFANewSouthWhales.html",
    "title": "COFA New South Whales ",
    "img": "http://ielts.progmic.com/images/uni/1340407904.jpg",
    "url": "http://ielts.progmic.com/app/COFANewSouthWhales.html",
    "desc": "The College offers nine undergraduate degrees including some double degrees associated with other UNSW faculties.",
    "img2": "http://ielts.progmic.com/images/uni/thumb/1340407904.jpg"
  },
  {
    "url2": "http://ielts.progmic.com/app/i_RoyalCollegeOfArts.html",
    "title": "Royal College Of Arts ",
    "img": "http://ielts.progmic.com/images/uni/1340542224.jpg",
    "url": "http://ielts.progmic.com/app/RoyalCollegeOfArts.html",
    "desc": "The Royal College of Art (informally the RCA) is a public research university specialised in art and design located in London, United Kingdom. It is the world's only wholly postgraduate university of art and design, offering the degrees of Master of Arts (M.A.), Master of Philosophy (M.Phil.) and Doctor of Philosophy (Ph.D.). It was founded in 1837",
    "img2": "http://ielts.progmic.com/images/uni/thumb/1340542224.jpg"
  },
  {
    "url2": "http://ielts.progmic.com/app/i_MIDDLESEXUNIVERSITY.html",
    "title": "MIDDLESEX UNIVERSITY ",
    "img": "http://ielts.progmic.com/images/uni/1340410005.jpg",
    "url": "http://ielts.progmic.com/app/MIDDLESEXUNIVERSITY.html",
    "desc": "We have a reputation for the highest quality teaching, research that makes a real difference to people’s lives and a practical, innovative approach to working with businesses to develop staff potential and provide solutions to business issues. Our expertise is wide ranging, from engineering, information, health and social sciences, to business, arts and education - and we’re national leaders in work based learning solutions.",
    "img2": "http://ielts.progmic.com/images/uni/thumb/1340410005.jpg"
  },
  {
    "url2": "http://ielts.progmic.com/app/i_UNIVERSITYOFSCOTLAND.html",
    "title": "UNIVERSITY OF SCOTLAND ",
    "img": "http://ielts.progmic.com/images/uni/1340410189.jpg",
    "url": "http://ielts.progmic.com/app/UNIVERSITYOFSCOTLAND.html",
    "desc": " Founded in 1451, Glasgow is the fourth-oldest university in the English-speaking world. Over the last five centuries and more, we’ve constantly worked to push the boundaries of what’s possible. We’ve fostered the talents of seven Nobel laureates, one Prime Minister and Scotland’s inaugural First Minister. We’ve welcomed Albert Einstein to give a lecture on the origins of the general theory of relativity. Scotland’s first female graduates completed their degrees here in 1894 and the world’s first ultrasound images of a foetus were published by Glasgow Professor Ian Donald in 1958. In 1840 we became the first university in the UK to appoint a Professor of Engineering, and in 1957, the first in Scotland to have an electronic computer. All of this means that if you choose to work or study here, you’ll be walking in the footsteps of some of the world’s most renowned innovators, from scientist Lord Kelvin and economist Adam Smith, to the pioneer of television John Logie Baird.",
    "img2": "http://ielts.progmic.com/images/uni/thumb/1340410189.jpg"
  }
]

您可以将JSON复制并粘贴到一个文件中,以各种编码保存它,并查看代码的运行情况。UTF-8应该是一个不错的选择。

我添加了这个方法来从检索到的字符串中删除未转换的字符:

- (NSString *)stringByRemovingControlCharacters: (NSString *)inputString 
{ 
    NSCharacterSet *controlChars = [NSCharacterSet controlCharacterSet]; 
    NSRange range = [inputString rangeOfCharacterFromSet:controlChars]; 
    if (range.location != NSNotFound) { 
        NSMutableString *mutable = [NSMutableString stringWithString:inputString]; 
        while (range.location != NSNotFound) { 
            [mutable deleteCharactersInRange:range]; 
            range = [mutable rangeOfCharacterFromSet:controlChars]; 
        } 
        return mutable; 
    } 
    return inputString; 
} 

收到NSData后,我将其转换为NSString,调用上述方法以获取具有删除的控制字符的新字符串,然后再次将新NSString转换为NSData以进行进一步处理。

在我使用您的方法后,iOS 7出现此错误:error Domain=NSCOCAERRORDOMAIN Code=3840“操作无法完成。(可可错误3840。)(字符12171周围的对象格式错误。)UserInfo=0x10a30b020{NSDebugDescription=字符12171周围的对象格式错误。}先生,您刚刚救了我的命!谢谢您