Swift - 解析某些数据

时间:2018-04-23 06:25:22

标签: ios json swift parsing

需要从以下API响应中提取a的数据。从那里我需要提取每个bdata from the server [ { "id": 226, "name": "chorename", "desc": "{\n \"questions\" : [\n {\n \"b\" : 2,\n \"a\" : 1\n },\n {\n \"b\" : 3,\n \"a\" : 2\n },\n {\n \"b\" : 2,\n \"a\" : 8\n },\n {\n \"b\" : 9,\n \"a\" : 7\n },\n {\n \"b\" : 3,\n \"a\" : 6\n }\n ],\n \"operation\" : \"addition\"\n}", "reward": "1.00", "sched": "2018-04-19T15:54:24.657644+08:00", "parent": "shit", "type": "homework", "child": "", "occurrence": { "name": "once" }, "status": { "name": "ongoing" }, "date_created": "2018-04-23T14:16:35.739436+08:00", "date_modified": "2018-04-23T14:16:35.790237+08:00" } ] code on getting request 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 { var a = self.getAllDetail.append(item ) print("shit:" , a) // if let title = item["title"] as? String { // self.titleArray.append(title) // } } if !self.getAllDetail.isEmpty{ DispatchQueue.main.async { } } break case .failure(_): print("Error") break } } } let devices = assign "a" here let randomIndex = Int(arc4random_uniform(UInt32(devices.count))) let randomItem = devices[randomIndex] print("random item:" ,randomItem) let device2 = //assign "b" here let randomIndex1 = Int(arc4random_uniform(UInt32(devices.count))) let randomItem1 = device2[randomIndex] ,以便我可以将其用于数学加法应用程序。

 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]]

API响应:

...
Application.Wait (Now + TimeValue("0:00:01"))
session.findById("wnd[0]/usr/ctxtMATNR-LOW").text = "500000000"
...

1 个答案:

答案 0 :(得分:0)

以下代码可用于将json stringstring转换为dictionary

<强>目标c

+ (NSDictionary *)dictionaryFromJSONString:(NSString *)jsonString
{
    // @"{\"2\":\"3\"}"
    NSError *jsonError;
    NSData *objectData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData
                                                         options:NSJSONReadingMutableContainers
                                                           error:&jsonError];
    return json;
}

<强> SWIFT

func getDictionaryFromString(_ jsonString: String) -> [String:Any]? {

        let objectData = jsonString.data(using: .utf8)
        var dict: [String:Any]?

        if objectData != nil {
            do {
                dict = try JSONSerialization.jsonObject(with: objectData!, options:  .allowFragments) as? [String : Any]

            } catch {
                print("Error occured.")
            }
        }
        return dict
    }

创建字典后,您可以使用密钥访问该值。