ios - 快速解析 desc 并解析所选元素

标签 ios swift parsing

快速解析 desc 并解析所选元素。我想解析 desc 并在解析 desc 后解析 desc 内容。感谢快速解析 desc 和解析所选元素。

我尝试过不同的方法。有谁知道我们如何才能实现这一目标?我尝试了以下代码:

**Code for getting the data**

 func demoApi1() {
        Alamofire.request("", method: .get, parameters: nil, encoding: JSONEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in

            switch(response.result) {
            case .success(_):
                guard let json = response.result.value as! [[String:Any]]? else{ return}
                print("Ang Response: , \(json)")

                for item in json {

                  self.getAllDetail.append(item )


                    if let desc = item["dec"] as? String {
                        self.titleArray.append(desc)

                        print("motherfucker:" , self.titleArray)

                    }

                }
                if !self.getAllDetail.isEmpty{
                    DispatchQueue.main.async {

                    }
                }
                break

            case .failure(_):
                print("Error")
                break

            }
        }

    }

回应:

 Ang Response [["status": {
    name = ongoing;
}, "sched": 2018-04-10T14:22:00+08:00, "desc": asdasdasdsa, "id": 224, "reward": 1.00, "parent": das, "child": dasdas, "date_created": 2018-04-19T15:54:24.657644+08:00, "name": sad, "occurrence": {
    name = once;
}, "type": , "date_modified": 2018-04-19T15:54:24.703520+08:00], ["status": {
    name = ongoing;
}, "sched": 2018-04-19T15:54:24.657644+08:00, "desc": {
  "questions" : [
    {
      "b" : 2,
      "a" : 1
    },
    {
      "b" : 3,
      "a" : 2
    },
    {
      "b" : 2,
      "a" : 8
    },
    {
      "b" : 9,
      "a" : 7
    },
    {
      "b" : 3,
      "a" : 6
    }
  ],
  "operation" : "addition"
}, "id": 226, "reward": 1.00, "parent": shit, "child": , "date_created": 2018-04-23T14:16:35.739436+08:00, "name": chorename, "occurrence": {
    name = once;
}, "type": homework, "date_modified": 2018-04-23T14:16:35.790237+08:00]]

最佳答案

字符串到字典:

   func stringToDictionary(_ strToJSON : String)-> [String:Any]{

    print("JsonString:\(strToJSON)")

    let data = strToJSON.data(using: .utf8)
    var dict = [String:Any]()
    do {
        dict = try JSONSerialization.jsonObject(with: data!) as! [String:Any] 
        return dict
    }
    catch let error as NSError {
        print("Error is:\(error)")
    }

    return dict
}

像这样使用它:

let dictConv = stringToDictionary("your_string")
print(dictConv)

关于ios - 快速解析 desc 并解析所选元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49977962/

相关文章:

objective-c - 在 iOS5 上像雅虎财经小部件一样滚动文本

ios - 如何根据Firestore数据在collectionView中显示行和节标题?

json - 使用 Codable swift 解析时忽略数组中的空对象

java - 将字符串解析为带前导零的长

ios - 定时器有按钮功能吗?

ios - 在自定义 UIGestureRecognizer 类中调用 self.init 之前使用的 self

ios - 领英 SDK ios swift

ios - Firebase 中的嵌套键查询?

ios - Swift:如果使用 DropDown(),应用程序会崩溃

java - AST如何处理空节点