从NSDictionary获取JSON数据

时间:2018-04-11 06:57:26

标签: ios json swift xcode nsdictionary

我的api中有结构

.tabs-nav .tab-active a {
  border-bottom: 3px solid red;
  box-sizing: border-box;
  cursor: default;
}

现在我被困住了,无法拉出价格和时间。我知道有很多问题,但我仍然无法理解,请帮忙。

我使用此代码:

{
    "Hall":"Hall",
    "Date":20180501,
    "Prices":[
        {
            "Time":1,
            "Price":4000
        },
        {
            "Time":2,
            "Price":4000
        },
        {
            "Time":3,
            "Price":4000
        }
    ]
}

我是json的新手,刚开始学习它。我知道这很容易,但我无法弄清楚。我也知道我可以使用可编码和可解码的,但现在我需要在这个实现中获得价格和时间。

4 个答案:

答案 0 :(得分:4)

首先不要使用NSArray / NSDictionary ,请使用原生的Swift类型。

Prices的值是[String:Int]词典数组:

if let jsonResult = try JSONSerialization.jsonObject(with: data!) as? [String:Any], 
   let prices = jsonResult["Prices"] as? [[String:Int]] {
    for price in prices {
        print(price["Time"]!, price["Price"]!)
    }
}

但是我建议将JSON解码为一个在Swift 4中非常简单的结构

struct Item : Decodable {
    let hall : String
    let date : Int
    let prices : [Price]

    private enum CodingKeys : String, CodingKey { case hall = "Hall",  date = "Date", prices = "Prices"}
}

struct Price  : Decodable {
    let time, price : Int

    private enum CodingKeys : String, CodingKey { case time = "Time",  price = "Price"}
}
do {
    let result = try JSONDecoder().decode(Item.self, from: data!)
    print(result)
} catch { print(error) }

答案 1 :(得分:0)

产品结构:

MainActivity

班级变量:

struct Product{

    let time:Int
    let price:Int

    init(_ object:[String:Int]){

        self.time = object["Time"] ?? 0
        self.price = object["Price"] ?? 0
    }
}

JSON解析:

var products = [Product]()

现在,您的do{ if let jsonObject = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:Any] { //Use Swift dictionary instead of NSDictionary if let prices = jsonObject["Prices"] as? [[String:Int]]{ for price in prices{ self.products.append(Product(price)) } } } }catch{ print("Error: ",error.localizedDescription) } products将包含所有ArrayPrice

示例:

Time

<强>输出:

  

时间:1

     

价格:4000

     

时间:2

     

价格:4000

     

时间:3

     

价格:4000

注意:为了更好地理解,这是关于swift 4中JSON解析的video系列

答案 2 :(得分:-1)

    var timeArray = [String]()
    var priceArray = [String]()
    if  jsonResult.object(forKey:"Prices") as? NSArray != nil
    {
        let pricesArray = jsonResult.object(forKey:"Prices") as! NSArray
        for i in 0..<self.pricesArray.count
        {
            // Getting Time
            if jsonResult.object(forKey:"Time") as? Int != nil
            {
                self.timeArray.append(jsonResult.object(forKey:"Time") as! Int)
            }
            else
            {
                print("Time is not a intrger")
            }
            // Getting Price
            if jsonResult.object(forKey:"Price") as? Int != nil
            {
                self.priceArray.append(jsonResult.object(forKey:"Price") as! Int)
            }
            else
            {
                print("Price is not a intrger")
            }
        }
    }
    else
    {
        print("Empty Array")
    }

答案 3 :(得分:-1)

int32_t value;
uint8_t extension = byte2 & 0x80 ? 0xff:00; /* checks bit 7 */
value = (int32_t)byte0 | ((int32_t)byte1 << 8) | ((int32_t)byte2 << 16) | ((int32_t)extension << 24);