在Objective-C中将NSString转换为NSDictionary

时间:2016-08-11 06:59:33

标签: ios objective-c json nsstring nsdictionary

这是我的字符串

     {
       ColorModel = RGB;
       DPIHeight = 72;
       DPIWidth = 72;
       Depth = 8;
       Orientation = 1;
       PixelHeight = 2848;
       PixelWidth = 4288;
       ProfileName = "Adobe RGB (1998)";
"{Exif}" =     {
       ColorSpace = 1;
       ComponentsConfiguration =         (
        1,
        2,
        3,
        0
    );
    DateTimeDigitized = "2011:03:12 16:17:25";
    DateTimeOriginal = "2011:03:12 16:17:25";
    ExifVersion =         (
        2,
        2
    );
    ExposureBiasValue = 0;
    ExposureProgram = 3;
    ExposureTime = "0.000625";
    FNumber = 4;
    Flash = 0;
    FlashPixVersion =         (
        1,
        0
    );
    FocalLength = 26;
    ISOSpeedRatings =         (
        200
    );
    LightSource = 14;
    MaxApertureValue = 4;
    MeteringMode = 5;
    PixelXDimension = 4288;
    PixelYDimension = 2848;
    SceneCaptureType = 0;
    SensingMethod = 2;
};
"{GPS}" =     {
    Latitude = "38.0374445";
    LatitudeRef = N;
    Longitude = "122.8031783333333";
    LongitudeRef = W;
};
"{IPTC}" =     {
    DateCreated = 20110312;
    DigitalCreationDate = 20110312;
    DigitalCreationTime = 161725;
    ObjectName = "DSC_0001";
    TimeCreated = 161725;
};
"{JFIF}" =     {
    DensityUnit = 1;
    JFIFVersion =         (
        1,
        0,
        1
    );
    XDensity = 72;
    YDensity = 72;
};
"{TIFF}" =     {
    DateTime = "2011:05:03 20:30:44";
    Make = "NIKON CORPORATION";
    Model = "NIKON D90";
    Orientation = 1;
    PhotometricInterpretation = 2;
    ResolutionUnit = 2;
    Software = "QuickTime 7.7.1";
    XResolution = 72;
    YResolution = 72;
};
}

我试图通过使用下面的代码

将其转换为NSDictionary
  NSData *myData = [myStr dataUsingEncoding:NSUTF8StringEncoding];
  NSDictionary *json = [NSJSONSerialization JSONObjectWithData:myData
                                                             options:kNilOptions
                                                               error:&err];

但最后 null

我的问题是字符串格式化...如何更改我的字符串格式以将其转换为NSDictionary

编辑:收到此错误

Error Domain=NSCocoaErrorDomain Code=3840 "No string key for value in object around character 6." UserInfo={NSDebugDescription=No string key for value in object around character 6.}

任何人请?

2 个答案:

答案 0 :(得分:0)

试试这个:

NSError *err = nil;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData: myData options: NSJSONReadingMutableContainers error: &err];

答案 1 :(得分:0)

那不是JSON;它看起来更像是[NSDictionary description]的输出。

为了获得接近JSON的东西,您需要:

  • 将所有=个字符更改为:,以提供name : value
  • 在元素之间将;更改为,
  • 将所有数组分隔符从( ... )更改为[ ... ]
  • 在所有非数字值周围添加双引号。