【问题标题】:While decoding json from webservice, it shows an error: Could not cast value of type '__NSArrayM' (0x10b84bdb0) to 'NSDictionary' (0x10b84c288)从 web 服务解码 json 时,它显示错误:无法将类型“__NSArrayM”(0x10b84bdb0)的值转换为“NSDictionary”(0x10b84c288)
【发布时间】:2017-10-21 19:31:55
【问题描述】:

我的代码:

var nameArray = [String]()


do
{

  let arrayValues = try! JSONSerialization.jsonObject(with: data! as Data, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSDictionary

                    print(arrayValues.count)
                    print(arrayValues)

                    let limit = arrayValues.count-1

                    for i in 0...limit
                    {
                         let x = i

                        let dataValues = arrayValues.object(forKey: "result" ) as! NSDictionary        // shows error

                        print(dataValues)

                       let name = dataValues.object(forKey: "description")

                        self.nameArray.insert(name as! String, at: x)




                    }

来自网络服务的响应

 {
   "result": [
      {
         "id": "1",
         "title": "Mental Health",
         "description": "<p>Mental health<\/p>",
         "date": "22 Jul 2015",
         "image": "image1444714284.jpg",
         "status": "0"
      },
      {
         "id": "2",
         "title": "Olive oil: The healthiest fat is also harmful for you",
         "description": "<p>ssss<\/p>",
         "date": "16 Jul 2015",
         "image": "image1444714355.jpg",
         "status": "1"
      },
      {
         "id": "3",
         "title": "Jogging:Find the best fitness friend",
         "description": "<p>ji<\/p>",
         "date": "16 Jul 2015",
         "image": "image1444714465.jpg",
         "status": "1"
      },
      {
         "id": "4",
         "title": "Stock up on these",
         "description": "<p>fffffffff<\/p>",
         "date": "15 Jul 2015",
         "image": "image1444715133.jpg",
         "status": "1"
      },
      {
         "id": "5",
         "title": "Pick your perfect tunes",
         "description": "<p>fffffffff<\/p>",
         "date": "15 Jul 2015",
         "image": "image1444715295.jpg",
         "status": "0"
      },
      {
         "id": "6",
         "title": "Buy comfy sneaks",
         "description": "<p>h<\/p>",
         "date": "22 Jul 2015",
         "image": "image1444715388.jpg",
         "status": "1"
      },
      {
         "id": "7",
         "title": "Eat this, run that",
         "description": "<p>rrrrrr<\/p>",
         "date": "9 Jul 2015",
         "image": "image1444715465.jpg",
         "status": "1"
      },
      {
         "id": "8",
         "title": "Say hello to H20",
         "description": "<p>jkjk<\/p>",
         "date": "10 Jul 2015",
         "image": "image1444715515.jpg",
         "status": "1"
      },
      {
         "id": "11",
         "title": "gfhsh",
         "description": "<p>sdhsgh fgdfgdgdf<\/p>",
         "date": "17 Dec 2015",
         "image": "image1450263806.jpg",
         "status": "1"
      },
      {
         "id": "12",
         "title": "test",
         "description": "<p>dfsdfsdffds<\/p>",
         "date": "16 Dec 2015",
         "image": "image1450264678.jpg",
         "status": "1"
      }
   ],
   "status": true,
   "message": "Health Tips List..."
}

【问题讨论】:

  • 描述在哪里?

标签: ios json swift web-services nsdictionary


【解决方案1】:

试试这个

var desriptionArray = [String]()

  for dataValues in responseObject["result"] as! [[String: AnyObject]]  {
      let name = dataValues ["description"] as! String
      desriptionArray .append(name)
   }

for (index , element) in (responseObject["result"] as! [Any]).enumerated() {
                        let nameDict = element  as! [String : AnyObject]
                        let strDecription = nameDict["description"] as! String
                        desriptionArray .insert(strDecription, at: index)
                    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多