如何在ios中解析这种类型的JSON响应?

时间:2014-04-13 12:46:05

标签: ios objective-c json

我是iOS新手。我想在iOS中解析那种类型的JSON响应。这是PayPal的回复。我想访问所有数据并制作字典并将其存储用于其他目的。如何访问所有字段并使其成为字典?

{

    client =     {

        environment = sandbox;

        "paypal_sdk_version" = "2.0.1";

        platform = iOS;

        "product_name" = "PayPal iOS SDK";

    };

    response =     {

        "create_time" = "2014-04-12T05:15:25Z";

        id = "PAY-72670124ZX823130UKNEMX3I";

        intent = sale;

        state = approved;

    };

    "response_type" = payment;

}

1 个答案:

答案 0 :(得分:5)

您需要使用JSONObjectWithData:options:error:

解析JSON
NSData *data = responseData; // Data from HTTP response
NSError *error;
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];

if (!dictionary) {
    // An error occured - examine 'error'
}

NSString *responseType = dictionary[@"response_type"]; // Will extract "payment" out of the JSON
NSDictionary *client = dictionary[@"client"];
NSDictionary *response = dictionary[@"response"];
NSString *paypalSDKVerion = client[@"paypal_sdk_version"];