服务器端Swift URLSession.shared.dataTask和JSONSerialization.jsonObject陷入困境。如何创建有效的字典?

时间:2018-07-29 18:51:59

标签: swift server-side kitura

也许它正盯着我,但是当我尝试填写Swift Dictionary为Kitura / Stencil网页进行渲染时,我一直遇到问题。

简而言之,我正在调用带有HTTP请求(REST API)的本地服务器,该请求会发送回JSON,然后尝试将其复制到要使用Stencil渲染的字典中。

Swift的新手,我试图弄清楚如何构造它。我已经尝试了十几个示例,但是每当我在字典上下文中复制返回的JSON时(将其馈入响应的render方法中),它似乎都是空的。

代码段为:

  router.get("/") {
  request, response, next in
  defer { next() }

  let todoEndpoint = "http://localhost:8090/"

  var context = [String: Any]()
  guard let url = URL(string: todoEndpoint) else { return }

  URLSession.shared.dataTask(with: url) { (data, response, error)
    in
    // perhaps err handling & response checking should be added
    guard let data = data else { return  }
    do {
     let json = try JSONSerialization.jsonObject(with: data) as! [String: Any]
      print ("\n json is \(json)")

      let info = json["Result1"] as! [Any]
      //  print ("info is \(info)")
        context["results"] = info
        print ("\n context in the info = json - section: \(context)")

    } catch let jsonErr { print ("Json error occurred")}

  }.resume()
  print ("\n context just before the response: \(context)")
  try response.render("home", context: context)
}

打印语句是-在我的Linux终端中出现的顺序:

在响应之前的

上下文:[:]

json是

[
  "Result1": [
    [
      "stockNumber": "1",
      "instock": "45",
      "description": "survival tent",
      "price": "250.0",
      "replenVal": "15"
    ],
    [
      "stockNumber": "2",
      "instock": "35",
      "description": "hiking shoes",
      "price": "150.0",
      "replenVal": "20"
    ],
    [
      "stockNumber": "3",
      "instock": "67",
      "description": "flash light P12",
      "price": "15.0",
      "replenVal": "50"
    ],
    [
      "stockNumber": "4",
      "instock": "34",
      "description": "flash light P15",
      "price": "12.0",
      "replenVal": "50"
    ],
    [
      "stockNumber": "5",
      "instock": "50",
      "description": "flash light P18",
      "price": "82.0",
      "replenVal": "50"
    ],
    [
      "stockNumber": "6",
      "instock": "50",
      "description": "light weight tent",
      "price": "124.0",
      "replenVal": "50"
    ],
    [
      "stockNumber": "7",
      "instock": "5",
      "description": "sleeping bag polar",
      "price": "349.0",
      "replenVal": "5"
    ],
    [
      "stockNumber": "8",
      "instock": "25",
      "description": "sleeping bag eco",
      "price": "149.0",
      "replenVal": "5"
    ],
    [
      "stockNumber": "9",
      "instock": "85",
      "description": "sleeping bag XL",
      "price": "249.0",
      "replenVal": "25"
    ],
    [
      "stockNumber": "10",
      "instock": "126",
      "description": "thermo mug",
      "price": "9.0",
      "replenVal": "50"
    ]
  ]
]

info = json-部分中的上下文:

[
  "results": [
    [
      "stockNumber": "1",
      "instock": "45",
      "description": "survival tent",
      "price": "250.0",
      "replenVal": "15"
    ],
    [
      "stockNumber": "2",
      "instock": "35",
      "description": "hiking shoes",
      "price": "150.0",
      "replenVal": "20"
    ],
    [
      "stockNumber": "3",
      "instock": "67",
      "description": "flash light P12",
      "price": "15.0",
      "replenVal": "50"
    ],
    [
      "stockNumber": "4",
      "instock": "34",
      "description": "flash light P15",
      "price": "12.0",
      "replenVal": "50"
    ],
    [
      "stockNumber": "5",
      "instock": "50",
      "description": "flash light P18",
      "price": "82.0",
      "replenVal": "50"
    ],
    [
      "stockNumber": "6",
      "instock": "50",
      "description": "light weight tent",
      "price": "124.0",
      "replenVal": "50"
    ],
    [
      "stockNumber": "7",
      "instock": "5",
      "description": "sleeping bag polar",
      "price": "349.0",
      "replenVal": "5"
    ],
    [
      "stockNumber": "8",
      "instock": "25",
      "description": "sleeping bag eco",
      "price": "149.0",
      "replenVal": "5"
    ],
    [
      "stockNumber": "9",
      "instock": "85",
      "description": "sleeping bag XL",
      "price": "249.0",
      "replenVal": "25"
    ],
    [
      "stockNumber": "10",
      "instock": "126",
      "description": "thermo mug",
      "price": "9.0",
      "replenVal": "50"
    ]
  ]
]

我已经检查了来自后端服务器的JSON是否有效。 我不明白为什么:

  • 打印输出以空的 context 字典开头,而我希望它是最后一个
  • 为什么在do-catch中填充 context 词典而在do-catch外部为空
  • 有什么想法可以使它正常工作吗?

非常感谢

0 个答案:

没有答案