Ios 在Objective-C中将NSString转换为NSDictionary
这是我的绳子Ios 在Objective-C中将NSString转换为NSDictionary,ios,objective-c,json,nsstring,nsdictionary,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}" = { ColorS
{
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.}
有人吗?试试这个:
NSError *err = nil;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData: myData options: NSJSONReadingMutableContainers error: &err];
这不是JSON;它看起来更像是
[NSDictionary description]
的输出
为了接近JSON,您需要:
- 将所有
字符更改为=
,以提供:
名称:值
- 更改
代码>到元素之间的
,
- 将所有数组分隔符从
更改为(…)
[…]
- 在所有非数值周围添加双引号
错误参数-您传递给nil
的参数?使用它,而不是传递nil
,然后查看问题所在。您的JSON字符串格式似乎不正确。您没有JSON字符串。你有字典。@rmaddy更新了我的问题。你能查一下吗once@AnupamMishra好的……我该如何更改它您省略了下面几行:if(!json)NSLog(@“Bad json:%@”,err)
@rmaddy我的目的只是显示json序列化选项并使用NSError。无论如何,谢谢你的提示。@VishalSonawane同样的问题。错误的JSONTry将字符串放入联机JSON查看器:并检查其格式是否正确。@VishalSonawane获取无效的JSON变量。如何解决这个问题?所有的事情都没问题。但是如何在所有非数值周围加上双引号呢。