Ios 如何在Swift中将'\u nsiarayi'转换为JSON数组?

Ios 如何在Swift中将'\u nsiarayi'转换为JSON数组?,ios,json,swift,nsarray,alamofire,Ios,Json,Swift,Nsarray,Alamofire,我是Swift 4的新手,正在开发iOS应用程序。我从服务器检索了以下JSON数据,这些数据将被提取到数据表中 我在navdata函数中完成了以下任务,以将“\uu nsiarayi”检索到JSON: func getNAVData(){ Alamofire.request(url, method: .post).responseJSON{ response in if response.result.isSuccess{

我是Swift 4的新手,正在开发iOS应用程序。我从服务器检索了以下JSON数据,这些数据将被提取到数据表中

我在navdata函数中完成了以下任务,以将“\uu nsiarayi”检索到JSON:

func getNAVData(){
        Alamofire.request(url, method: .post).responseJSON{
            response in
            if response.result.isSuccess{
                print("Success")
                //let navDataJSON : JSON = JSON(response.result.value!
                let navDataJSON : JSON = JSON(response.result.value!)

                print(navDataJSON)
            }else{
                print("Failed")
            }
        }

    }

我需要将其转换为JSON数组,这是用java完成的。如何在Swift 4中执行类似的任务?

您的方法是正确的,在这里您需要将JSON响应转换为数组,并检查JSON响应是否包含该值。请尝试下面的代码

 if response.result.isSuccess{
            print("Success")
            // convert your JSON to swiftyJSON array type if your JSON response as array as well as check its contains data or not
             guard let resJson = JSON(responseObject.result.value!).array ,  !resJson.isEmpty else { return
            }
            // create the Swifty JSON array for global access like wise
            var navDataJSON  =  [JSON]() //[JSON]() --> Swifty-JSON array memory allocation.
            navDataJSON =  resJson


        }

如果要使用SwiftyJSON,请返回responseData而不是responseJSON。这避免了双重转换,并始终处理潜在的错误

func getNAVData(){
    Alamofire.request(url, method: .post).responseData { response in

        switch response.result {
            case .success(let data):
                 let navDataJSON = JSON(data).arrayValue
                 print(navDataJSON)                 

             case .failure(let error): print(error)
        }
    }
}
结果navDataJSON是一个JSON数组

但是,在Swift 4+中,强烈建议使用更高效的内置可编码协议


结果是一个基金结构数组。

您将获得JSON数组,如果您想转换为模型数组,则可以使用创建模型结构并确认可解码协议,而无需使用SwiftyJSON使用内置解决方案解析JSON

import Foundation

// MARK: - Element
struct Model: Decodable {
    let fundNavValidity: String
    let fundNavCost: Int
    let fundNavSell: Double
    let navId: Int
    let fundNavBuy: Int
    let fundId: Int
    let navAsOnDate: String
    let fundNavMarket: Double

    enum CodingKeys: String, CodingKey {
        case fundNavValidity = "fund_nav_validity"
        case fundNavCost = "fund_nav_cost"
        case fundNavSell = "fund_nav_sell"
        case navId = "nav_id"
        case fundNavBuy = "fund_nav_buy"
        case fundId = "fund_id"
        case navAsOnDate = "nav_as_on_date"
        case fundNavMarket = "fund_nav_market"
    }
} 
然后在getNAVData方法中,您可以使用JSONDecoder转换为如下所示的结构数组模型

func getNAVData() {
        Alamofire.request(url, method: .post).responseJSON {
            response in
            switch response.result {
            case .success(let data):
                do {
                    let modelArray = try JSONDecoder().decode([Model].self, from: data)
                    print(modelArray)
                } catch {
                    print("Error: \(error)")
                }
            case .failure(let error):
                print(error.localizedDescription)
            }
        }
    }

显示您尝试过的代码。我已添加代码。基本上,我没有尝试将其转换为JSON数组,因为我不知道正确的方法。添加一些附加代码这不是JSON数组吗?在swift中解码JSON时,您应该考虑使用Codable,这里有很多类似的问题要看,还有许多在线教程和文章。所以,先做一些研究,如果你被卡住了就回来。下面是一篇来自苹果的文章,让你开始,.拒绝投票的原因,没有理由不要给出拒绝投票,如果它是有效的,我准备更新答案]没有解释代码的作用以及它如何帮助OP,我认为我们应该期待一个拥有67k+声誉的人能做得更好。既然swift对json解码的内置支持非常好,为什么还要使用swiftyjson。@JoakimDanielson-感谢,我更新了答案,请检查,在这里我们可以建议使用codeable,但我们不能强制使用,基于开发者偏好,它将改变@Anbu.Karthik提供的解决方案。感谢您在代码中添加解释。如上所述,OP对swift来说是新的,因此,令人遗憾的是,接受答案中的解决方案是建立在第三方库上的,这不是swift社区的首选解决方案,但我想这就是它的方式。
import Foundation

// MARK: - Element
struct Model: Decodable {
    let fundNavValidity: String
    let fundNavCost: Int
    let fundNavSell: Double
    let navId: Int
    let fundNavBuy: Int
    let fundId: Int
    let navAsOnDate: String
    let fundNavMarket: Double

    enum CodingKeys: String, CodingKey {
        case fundNavValidity = "fund_nav_validity"
        case fundNavCost = "fund_nav_cost"
        case fundNavSell = "fund_nav_sell"
        case navId = "nav_id"
        case fundNavBuy = "fund_nav_buy"
        case fundId = "fund_id"
        case navAsOnDate = "nav_as_on_date"
        case fundNavMarket = "fund_nav_market"
    }
} 
func getNAVData() {
        Alamofire.request(url, method: .post).responseJSON {
            response in
            switch response.result {
            case .success(let data):
                do {
                    let modelArray = try JSONDecoder().decode([Model].self, from: data)
                    print(modelArray)
                } catch {
                    print("Error: \(error)")
                }
            case .failure(let error):
                print(error.localizedDescription)
            }
        }
    }