2010-12-01 56 views
0

我已經複製JSON.framework /軟件開發工具包/ iPhoneSimulator4.0.sdk /系統/庫/框架JSON.framework在我</p> <p>開發者/平臺/ iPhoneSimulator.platform /開發人員添加在Xcode

/開發人員/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.0.sdk/System/Library/Frameworks

夾和進口

#import <JSON/SBJsonParser.h> 

,但是當我用它給我鏈接錯誤錯誤 命令/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2失敗,退出代碼1

Ld的構建/調試-iphonesimulator/HelloThere.app/HelloThere正常I386 CD /用戶/ samargupta/Desktop/hellothere setenv MACOSX_DEPLOYMENT_TARGET 10.6 setenv PATH「/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin」 /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2 -arch i386 -isysroot /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.0.sdk -L/Users/samargupta/Desktop/hellothere/build/Debug-iphonesimulator -F/Users/samargupta/Desktop/hellothere/build/Debug-iphonesimulator -filelist/Users/samargu pta/Desktop/hellothere/build/HelloThere.build/Debug-iphonesimulator/HelloThere.build/Objects-normal/i386/HelloThere.LinkFileList -mmacosx-version-min = 10.6 -Xlinker -objc_abi_version -Xlinker 2 -framework Foundation -framework UIKit -framework CoreGraphics在-framework CoreLocation -framework MapKit -framework JSON -o /Users/samargupta/Desktop/hellothere/build/Debug-iphonesimulator/HelloThere.app/HelloThere

LD:框架未發現JSON collect2:LD返回1退出狀態 Command /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2失敗,退出代碼爲1

plz告訴我我錯在哪裏?

回答

3

我自己寫了一個JSON解析器。如果你想通過將代碼複製到你的類源中,你可以使用它。

你以這種方式使用它:

NSObject *obj = [self jsonToObj:yourJsonString]; 

它返回一個NSDictionaryNSStringNSArray根據輸入。我的解析器與其他解析器的區別在於,它也接受數字鍵,這在JSON標準中是不允許的。我不能保證它裏面沒有錯誤,或者它可以與所有JSON字符串一起使用。請給我一些反饋,如果它適合你!

-(NSObject*)jsonToObj:(NSString *)json { 
    int nextOffset; 
    return [self jsonToObj:json offset:0 nextOffset:&nextOffset]; 
} 

static int indexOfChar(NSString *str, unichar c, int offset) { 
    int length = [str length]; 
    for (int i = offset; i < length; i++) { 
     if ([str characterAtIndex:i] == c) { 
      return i; 
     } 
    } 
    return -1; 
} 

static int indexOfNonWhitespace(NSString *str, int offset) { 
    int len = [str length]; 
    NSCharacterSet *cSet = [NSCharacterSet whitespaceAndNewlineCharacterSet]; 
    for (int i = offset; i < len; i++) { 
     if (![cSet characterIsMember:[str characterAtIndex:i]]) { 
      return i; 
     } 
    } 
    return -1; 
} 

static int indexOfNonNumber(NSString *str, int offset) { 
    int len = [str length]; 
    for (int i = offset; i < len; i++) { 
     unichar c = [str characterAtIndex:i]; 
     if (c != '-' && c != '.' && (c < '0' || c > '9')) return i; 
    } 
    return -1; 
} 

static int parseHex(NSString *hexStr) { 
    unsigned int result = 0; 
    int len = [hexStr length]; 
    for (int i = 0; i < len; i++) { 
     result <<= 4; 
     unsigned int v = 0; 
     unichar c = [hexStr characterAtIndex:i]; 
     if (c >= '0' && c <= '9') { 
      v = c-'0'; 
     } else if (c >= 'a' && c <= 'f') { 
      v = c-'a'+10; 
     } else if (c >= 'A' && c <= 'F') { 
      v = c-'A'+10; 
     } 
     result += v; 
    } 
    return result; 
} 

-(NSObject*)jsonToObj:(NSString *)json offset:(int)offset nextOffset:(int*)nextOffset { 
    offset = indexOfNonWhitespace(json, offset); 
    static NSString *jsonExceptionName = @"InvalidJSONException"; 
    if (offset == -1) { 
     @throw [NSException exceptionWithName:jsonExceptionName reason:@"no non-whitespace 1" userInfo:nil]; 
    } 
    unichar startChar = [json characterAtIndex:offset]; 
    if (startChar == '{') { 

     NSMutableDictionary *result = [NSMutableDictionary dictionary]; 

     int curPos = offset+1; 
     while (YES) { 
      curPos = indexOfNonWhitespace(json, curPos); 
      if (curPos == -1) { 
       @throw [NSException exceptionWithName:jsonExceptionName reason:@"no non-whitespace 2" userInfo:nil]; 
      } 
      unichar curChar = [json characterAtIndex:curPos]; 
      if (curChar == ',') { 
       curPos++; 
       continue; 
      } else if (curChar == '}') { 
       *nextOffset = curPos+1; 
       return result; 
      } 
      unichar quotChar = curChar; 
      if ((quotChar != '\'' && quotChar != '"') || [json characterAtIndex:curPos-1]=='\\') { 
       quotChar = 0; 
      } 
      NSString *key = nil; 
      int semiIndex = 0; 
      if (quotChar != 0) { 
       int quotStart = curPos+1; 
       int quotEnd = quotStart; 
       while (YES) { 
        quotEnd = indexOfChar(json, quotChar, quotEnd); 
        if (quotEnd == -1) { 
         @throw [NSException exceptionWithName:jsonExceptionName reason:@"quotation-end not found 1" userInfo:nil]; 
        } 
        if ([json characterAtIndex:quotEnd-1] != '\\') break; 
        else quotEnd++; 
       } 
       key = [json substringWithRange:NSMakeRange(quotStart, quotEnd-quotStart)]; 
       semiIndex = indexOfChar(json, ':', quotEnd); 
       if (semiIndex == -1) { 
        @throw [NSException exceptionWithName:jsonExceptionName reason:@"semicolon not found 1" userInfo:nil]; 
       } 
      } else { 
       semiIndex = indexOfChar(json, ':', curPos); 
       if (semiIndex == -1) { 
        @throw [NSException exceptionWithName:jsonExceptionName reason:@"semicolon not found 2" userInfo:nil]; 
       } 
       key = [[json substringWithRange:NSMakeRange(curPos, semiIndex-curPos)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
      } 

      [result setObject:[self jsonToObj:json offset:semiIndex+1 nextOffset:&curPos] forKey:key]; 
     } 
    } else if (startChar == '[') { 

     NSMutableArray *result = [NSMutableArray array]; 

     int curPos = offset+1; 
     while (YES) { 
      curPos = indexOfNonWhitespace(json, curPos); 
      if (curPos == -1) { 
       @throw [NSException exceptionWithName:jsonExceptionName reason:@"no non-whitespace 3" userInfo:nil]; 
      } 
      unichar curChar = [json characterAtIndex:curPos]; 
      if (curChar == ',') { 
       curPos++; 
       continue; 
      } else if (curChar == ']') { 
       *nextOffset = curPos+1; 
       return result; 
      } 
      [result addObject:[self jsonToObj:json offset:curPos nextOffset:&curPos]]; 
     } 
    } else { 
     unichar quotChar = startChar; 
     if ((quotChar != '\'' && quotChar != '"') || [json characterAtIndex:offset-1]=='\\') { 
      quotChar = 0; 
     } 
     if (quotChar != 0) { 
      int quotStart = offset+1; 
      int quotEnd = quotStart; 
      while (YES) { 
       quotEnd = indexOfChar(json, quotChar, quotEnd); 
       if (quotEnd == -1) { 
        @throw [NSException exceptionWithName:jsonExceptionName reason:@"quotation-end not found 2" userInfo:nil]; 
       } 
       if ([json characterAtIndex:quotEnd-1] != '\\') break; 
       else quotEnd++; 
      } 
      *nextOffset = quotEnd+1; 
      NSString *str = [json substringWithRange:NSMakeRange(quotStart, quotEnd-quotStart)]; 
      str = [str stringByReplacingOccurrencesOfString:@"\\\"" withString:@"\""]; 
      NSMutableString *result = [NSMutableString stringWithCapacity:[str length]]; 
      int len = [str length]; 
      for (int i = 0; i < len; i++) { 
       unichar c = [str characterAtIndex:i]; 
       if (c != '\\') { 
        [result appendString:[NSString stringWithCharacters:&c length:1]]; 
       } else if (i+1 < len) { 
        unichar c2 = [str characterAtIndex:i+1]; 
        if (c2 == '\\') { 
         [result appendString:[NSString stringWithCharacters:&c2 length:1]]; 
         i++; 
        } else if (c2 == 'u') { 
         unichar cUni = parseHex([str substringWithRange:NSMakeRange(i+2, 4)]); 
         [result appendString:[NSString stringWithCharacters:&cUni length:1]]; 
         i += 5; 
        } 
       } 
      } 
      return result; 
     } else { 
      int numberEnd = indexOfNonNumber(json, offset); 
      if (numberEnd == -1) { 
       @throw [NSException exceptionWithName:jsonExceptionName reason:@"number-end not found" userInfo:nil]; 
      } 
      //BOOL isFloat = indexOfChar(json, '.', offset)!=-1; 
      NSString *numberStr = [json substringWithRange:NSMakeRange(offset, numberEnd-offset)]; 
      *nextOffset = numberEnd; 
      return numberStr; 
     } 
    } 
} 
+0

我真的很滿意這段代碼。它編譯沒有任何錯誤。感謝您的迴應。現在我得到的對象其實我通過這段代碼來解析Google Geocoading API。現在我會在這個對象中得到什麼。 – 2010-12-02 07:47:56

0

只需在您的XCode項目名稱中創建一個名爲「JSON」的文件夾即可。所有由JSON框架提供的.h和.m文件都必須包含在你的應用程序,構建,運行等等中。等等。

0

框架文件夾「JSON.framework」必須與您希望使用(iPhone模擬器,4.3的iPhoneOS),或你的構建將失敗,您報告的錯誤每個SDK框架文件夾下添加。或者,您可以將文件夾鏈接到Bourne提到的項目文件夾之外。無論哪種方式,Xcode需要知道每種構建編譯的框架。