Objective c 不使用顶级标签解析JSON

Objective c 不使用顶级标签解析JSON,objective-c,django,json,Objective C,Django,Json,我正在使用django活塞并在Objective-C中解析JSON输出,但是遇到了一些问题,因为输出不包含任何顶级标签。我遇到的每个示例都将数据解析到一个字典中,然后基于objectForKey生成一个数组:@“foo”,但在我的例子中,我没有可以解析的顶级文本 以下是活塞生成的JSON: [ { "text": "Pain Intensity", "question_number": 1, "id": 1 }, { "text": "Personal Ca

我正在使用django活塞并在Objective-C中解析JSON输出,但是遇到了一些问题,因为输出不包含任何顶级标签。我遇到的每个示例都将数据解析到一个字典中,然后基于objectForKey生成一个数组:@“foo”,但在我的例子中,我没有可以解析的顶级文本

以下是活塞生成的JSON:

[
{
    "text": "Pain Intensity", 
    "question_number": 1, 
    "id": 1
}, 
{
    "text": "Personal Care (washing, dressing, etc.)", 
    "question_number": 2, 
    "id": 2
}, 
{
    "text": "Lifting", 
    "question_number": 3, 
    "id": 3
}, 
{
    "text": "Walking", 
    "question_number": 4, 
    "id": 4
}, 
{
    "text": "Sitting", 
    "question_number": 5, 
    "id": 5
}
]

我想做的是得到一个包含id、text和question number属性的对象数组

有什么建议吗?

我用过这个。这是非常简单和有用的。对于教程检查这个

解析代码的示例如下,您只需导入
“JSON.h”
文件:

    NSString *jsonPath = [[NSBundle mainBundle] pathForResource:FILE_NAME ofType:FILE_EXTENSION];
    NSString *jsonString = [NSString stringWithContentsOfFile:jsonPath encoding:NSUTF8StringEncoding error:nil];
    NSArray *jsonArray = [NSArray arrayWithArray:[jsonString JSONValue]];
我用过这个。这是非常简单和有用的。对于教程检查这个

解析代码的示例如下,您只需导入
“JSON.h”
文件:

    NSString *jsonPath = [[NSBundle mainBundle] pathForResource:FILE_NAME ofType:FILE_EXTENSION];
    NSString *jsonString = [NSString stringWithContentsOfFile:jsonPath encoding:NSUTF8StringEncoding error:nil];
    NSArray *jsonArray = [NSArray arrayWithArray:[jsonString JSONValue]];
如果您使用的是iOS 5(适用于iOS应用程序)或OS 10.7(适用于Mac应用程序),那么NSJSONSerialization类就内置在:

NSArray *parsedJSON = [JSONObjectWithData:myData options:myOptions error:&error];
只要myData对象(一个NSData实例)包含使用支持的JSON编码之一的有效JSON字符串,并且该JSON字符串的顶级元素是一个数组,就像在您的示例中一样,这将起作用

使用示例JSON字符串执行此操作时,应该会得到一个包含5个字典的NSArray。所以你可以这样做:

NSMutableArray *newArray = [[NSMutableArray alloc] init];
for (int i = 0; i < [parsedJSON count]; i++) {
    MyClass *newObject = [[MyClass alloc] init];
    NSDictionary *questionDict = [parsedJSON objectAtIndex:i];
    [myObject setText:[questionDict objectForKey:@"text"]];
    [myObject setQuestionNumber:[questionDict objectForKey:@"question_number"]];
    [myObject setID:[questionDict objectForKey:@"id"]];
    [newArray addObject:newObject];
}
NSMutableArray*newArray=[[NSMutableArray alloc]init];
for(int i=0;i<[parsedJSON count];i++){
MyClass*newObject=[[MyClass alloc]init];
NSDictionary*questionDict=[parsedJSON objectAtIndex:i];
[myObject setText:[questionDict objectForKey:@“text”];
[myObject setQuestionNumber:[questionDict objectForKey:@“问题编号”];
[myObject setID:[questionDict objectForKey:@“id”];
[newArray addObject:newObject];
}
以上假设您定义了一个名为MyClass的类,该类的属性为text、questionNumber和id。

如果您使用的是iOS 5(用于iOS应用程序)或OS 10.7(用于Mac应用程序),则NSJSONSerialization类将直接内置于:

NSArray *parsedJSON = [JSONObjectWithData:myData options:myOptions error:&error];
只要myData对象(一个NSData实例)包含使用支持的JSON编码之一的有效JSON字符串,并且该JSON字符串的顶级元素是一个数组,就像在您的示例中一样,这将起作用

使用示例JSON字符串执行此操作时,应该会得到一个包含5个字典的NSArray。所以你可以这样做:

NSMutableArray *newArray = [[NSMutableArray alloc] init];
for (int i = 0; i < [parsedJSON count]; i++) {
    MyClass *newObject = [[MyClass alloc] init];
    NSDictionary *questionDict = [parsedJSON objectAtIndex:i];
    [myObject setText:[questionDict objectForKey:@"text"]];
    [myObject setQuestionNumber:[questionDict objectForKey:@"question_number"]];
    [myObject setID:[questionDict objectForKey:@"id"]];
    [newArray addObject:newObject];
}
NSMutableArray*newArray=[[NSMutableArray alloc]init];
for(int i=0;i<[parsedJSON count];i++){
MyClass*newObject=[[MyClass alloc]init];
NSDictionary*questionDict=[parsedJSON objectAtIndex:i];
[myObject setText:[questionDict objectForKey:@“text”];
[myObject setQuestionNumber:[questionDict objectForKey:@“问题编号”];
[myObject setID:[questionDict objectForKey:@“id”];
[newArray addObject:newObject];
}

以上假设您已经定义了一个名为MyClass的类,其属性为text、questionNumber和id。

尽管不建议使用try eval。尽管不建议使用try eval。现在肯定每个人都在使用内置的NSJSONSerialization类了吗?现在肯定每个人都在使用内置的NSJSONSerialization类了吗?