[英]SBJson parse attempt — objective C
我正在尝试解析以下JSon(上次检查时已通过验证):
{
"top_level" = (
{
"download" = "http:/target.com/some.zip";
"other_information" = "other info";
"notes" = (
{
obj1 = "some text";
obj2 = "more notes";
obj3 = "some more text still";
}
);
title = "name_of_object1";
},
{
"download" = "http:/target.com/some.zip";
"other_information" = "other info";
"notes" = (
{
obj1 = "some text";
obj2 = "more notes";
obj3 = "some more text still";
}
);
title = "name_of_object2";
},
{
"download" = "http:/target.com/some.zip";
"other_information" = "other info";
"notes" = (
{
obj1 = "some text";
obj2 = "more notes";
obj3 = "some more text still";
}
);
title = "name_of_object3";
}
);
}
我的尝试使用以下方法:
NSDictionary *myParsedJson = [myRawJson JSONValue];
for(id key in myParsedJson) {
NSString *value = [myParsedJson objectForKey:key];
NSLog(value);
}
错误:
-[__NSArrayM length]: unrecognized selector sent to instance 0x6bb7b40
问题:在我看来,JSon值使myParsedJson对象成为NSArray而不是NSDictionary。
如何遍历名为name_of_object的对象并访问每个嵌套字典? 我会以正确的方式进行操作吗?
NSLog的第一个参数必须是字符串。 尝试这个:
NSLog(@"%@", value);
您的value
不是字符串,只是因为您已经键入了它。 根据您发布的结构,您将有一个数组作为顶级对象。
NSDictionary *myParsedJson = [myRawJson JSONValue];
for(id key in myParsedJson) {
id value = [myParsedJson objectForKey:key];
NSLog(@"%@", value);
}
NSLog中的%@
语法使-description
方法按值调用; 此方法返回一个NSString。 这意味着您可以执行NSLog([value description]);
但这通常不是一个好主意。 (有人可能会制作恶意输入,从而使您的应用程序崩溃。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.