7

このJSONhttp ://www.progmic.com/ielts/retrive.phpを解析する必要があります。NSJSONSerializationを使用して実行すると、「1981年の文字の周りに制御文字がエスケープされていません」というエラーが発生します。

私は知る必要がある:

  • エスケープされていない制御文字は一体何ですか?リストか何かありますか?
  • このエラーを取り除くにはどうすればよいですか?最も簡単な方法は?

前もって感謝します。

4

2 に答える 2

18

取得した文字列からエスケープされていない文字を削除するために、このメソッドを追加しました。

- (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に再度変換してさらに処理します。

于 2012-06-25T15:36:51.807 に答える
0

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が進むべき道です。

于 2012-06-24T14:18:19.817 に答える