在Swift中使用Alamofire解析JSON响应

时间:2022-05-14 21:30:40

When using the Alamofire Framework, my responses don't seem to be getting parsed correctly. The JSON response I get has some keys that appear to not be strings, and I don't know how to reference them/get their values.

当使用Alamofire框架时,我的响应似乎没有得到正确的解析。我得到的JSON响应有一些键看起来不是字符串,我不知道如何引用它们/获取它们的值。

Here is the part of my code that makes the call:

下面是我的代码中调用的部分:

var url = "http://api.sandbox.amadeus.com/v1.2/flights/low-fare-search"
var params = ["origin": "IST", 
         "destination":"BOS", 
      "departure_date":"2014-10-15", 
   "number_of_results": 1, 
              "apikey": KEY]

Alamofire.request(.GET, url, parameters: params)
  .responseJSON { (_, _, json, _) in
    println(json)
  }
}

And here is the first section printout when that function is called

这是调用该函数时的第一部分打印输出

Optional({
  currency = USD;
  results = ({
      fare = {
        "price_per_adult" = {
          tax = "245.43";
          "total_fare" = "721.43";
        };
        restrictions = {
          "change_penalties" = 1;
          refundable = 0;
        };
        "total_price" = "721.43";
      };
    ...
  });
});

You'll notice that results is not "results", but "price_per_adult" is the correct format. Is there some step I'm missing? When I cast it to NSDictionary it doesn't do anything to help the key format either.

您会注意到结果不是“结果”,而是“price_per_adult”是正确的格式。我是不是漏掉了什么步骤?当我将它转换为NSDictionary时它也不会对键格式有所帮助。

I also tried the same endpoint in javascript and ruby, and both came back without problem, so I'm fairly confident that it is not the API that is causing problems.

我还在javascript和ruby中尝试了相同的端点,并且都没有出现问题,所以我很有信心不是API导致了问题。

1 个解决方案

#1


1  

Those keys are still Strings, that's just how Dictionarys are printlnd. It looks like it will surround the String in quotes when printing it only if it contains non-alphanumeric characters (_ in this case). You can test this by manually creating a Dictionary similar to the one you're getting back from your API request and then printing it:

这些键仍然是字符串,这就是Dictionarys的printlnd。看起来只有当字符串包含非字母数字字符(在本例中为_)时,它才会将字符串括在引号中。您可以手工创建一个与您从API请求中获得的字典类似的字典,然后打印它:

let test = [
    "currency": "USD",
    "results": [
        [
            "fare": [
                "price_per_adult": [
                    "tax": "245.43",
                    "total_fare": "721.43"
                ],
                "restrictions": [
                    "change_penalties": 1,
                    "refundable": 0
                ],
                "total_price": "721.43"
            ]
        ]
    ]
]

println(test)

Outputs:

输出:

{
    currency = USD;
    results = (
    {
            fare = {
                "price_per_adult" = {
                    tax = "245.43";
                    "total_fare" = "721.43";
                };
                restrictions = {
                    "change_penalties" = 1;
                    refundable = 0;
                };
                "total_price" = "721.43";
            };
        }
    );
}

#1


1  

Those keys are still Strings, that's just how Dictionarys are printlnd. It looks like it will surround the String in quotes when printing it only if it contains non-alphanumeric characters (_ in this case). You can test this by manually creating a Dictionary similar to the one you're getting back from your API request and then printing it:

这些键仍然是字符串,这就是Dictionarys的printlnd。看起来只有当字符串包含非字母数字字符(在本例中为_)时,它才会将字符串括在引号中。您可以手工创建一个与您从API请求中获得的字典类似的字典,然后打印它:

let test = [
    "currency": "USD",
    "results": [
        [
            "fare": [
                "price_per_adult": [
                    "tax": "245.43",
                    "total_fare": "721.43"
                ],
                "restrictions": [
                    "change_penalties": 1,
                    "refundable": 0
                ],
                "total_price": "721.43"
            ]
        ]
    ]
]

println(test)

Outputs:

输出:

{
    currency = USD;
    results = (
    {
            fare = {
                "price_per_adult" = {
                    tax = "245.43";
                    "total_fare" = "721.43";
                };
                restrictions = {
                    "change_penalties" = 1;
                    refundable = 0;
                };
                "total_price" = "721.43";
            };
        }
    );
}