解析没有对象名称的简单json字符串

时间:2013-12-19 17:43:48

标签: ios json parsing

这可能是一个非常简单的答案,但我找不到一个很好的教程来帮助我。我正在尝试解析一个json字符串,我从http帖子返回时json字符串在我记录时看起来像这样:

(
        {
        "ADD_CITY" = BEDFORD;
        "ADD_LINE1" = "100 COURT AVE";
        "ADD_LINE2" = "";
        "ADD_STATE" = IA;
        "ADD_ZIP" = 50833;
        "BUSINESS_NAME" = "";
        "FIRST_NAME" = JOHN;
        "LAST_NAME" = DOE;
    },
        {
        "ADD_CITY" = BEDFORD;
        "ADD_LINE1" = "101 COURT AVE";
        "ADD_LINE2" = "";
        "ADD_STATE" = IA;
        "ADD_ZIP" = 50833;
        "BUSINESS_NAME" = "";
        "FIRST_NAME" = BOB;
        "LAST_NAME" = JOHNSON;
    }
)

我正在尝试这样做,我可以将每个地方的每个值插入到我已经制作的coredata表中,但是我将其解析为可以使用的内容。我已经读过我必须使用NSJSONSerialization做一些事情。

修改

-(void)getLocations{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"code": @"123"};
[manager POST:@"website.com/getjson" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
    response = responseObject;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];
}

2 个答案:

答案 0 :(得分:0)

是。 NSJsonSerialization完成了这项工作。它将json转换为NSDictionary。

This为您提供了一个很好的例子。

答案 1 :(得分:0)

AFNetworking已经为您完成了这项工作(来自JSON的反序列化)。因此,您可以相应地枚举和创建对象,以便存储在CoreData中。

-(void)getLocations{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *parameters = @{@"code": @"123"};
    [manager POST:@"website.com/getjson" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
        response = responseObject;

        for (NSDictionary *obj in responseObject) {
            // Create any objects for Coredata here
        }

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
}