【问题标题】:How to parse this type of JSON Response in ios? [duplicate]如何在 ios 中解析这种类型的 JSON 响应? [复制]
【发布时间】:2014-04-13 12:46:05
【问题描述】:

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

{

    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;

}

【问题讨论】:

标签: ios objective-c json


【解决方案1】:

您需要使用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"];

【讨论】:

  • 您遗漏了错误检查。
  • @Rich.Paypal 响应是 NSdictionary JSON 格式。所以,有必要使用 NSJSon 解析吗?
  • 没有NSDictionary JSON 格式,JSON 总是在standard format - 所以是的,这是必要的! :)
  • @RiCH。我的问题是Paypal给我Response的字典。它是默认的将数据转换为字典。所以,我可以直接访问字典吗?
  • @user3528976 是的,如果 Paypals SDK 返回一个 NSDictionary 你只需要最后一行。
猜你喜欢
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 2019-02-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-05
  • 1970-01-01
  • 2016-08-20
相关资源
最近更新 更多