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

时间:2021-02-22 06:47:24

This may be a very simple answer but I cant find a good tutorial to help me. I am trying to parse a json string that I had returned from a http post the json string looks like this when I log it:

这可能是一个非常简单的答案,但我找不到一个很好的教程来帮助我。我正在尝试解析一个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;
    }
)

I am trying to make so I can insert each value of each place into a coredata table that I have made but I am getting stuck parsing this into something I can work with. I have read I have to do something with using NSJSONSerialization.

我试图这样做,所以我可以将每个地方的每个值插入到我已经制作的coredata表中,但是我很难将其解析成我可以使用的东西。我已经阅读过使用NSJSONSerialization我必须做的事情。

edit

-(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 个解决方案

#1


0  

AFNetworking already done this for you (the deserialization from the JSON). So you could just enumerate and create your objects accordingly for storing in CoreData.

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);
    }];
}

#2


0  

Yes. NSJsonSerialization does the job. It converts the json to a NSDictionary.

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

This gives you a fine example.

这给你一个很好的例子。

#1


0  

AFNetworking already done this for you (the deserialization from the JSON). So you could just enumerate and create your objects accordingly for storing in CoreData.

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);
    }];
}

#2


0  

Yes. NSJsonSerialization does the job. It converts the json to a NSDictionary.

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

This gives you a fine example.

这给你一个很好的例子。