1

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

简而言之,我正在使用发送回 JSON 的 HTTP 请求(REST API)调用本地服务器,并且我试图将其复制到我正在使用 Stencil 呈现的字典中。

Swift 新手,我正试图弄清楚如何构建它。我已经尝试了十几个示例,但是每当我在输入响应的渲染方法的字典上下文中复制返回的 JSON 时,它似乎是空的。

代码片段是:

  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 是否有效,确实如此。我不明白为什么:

  • 打印输出以空上下文字典开始,而我希望它是最后一个
  • 为什么上下文字典在 do-catch 中被填充,而在 do-catch 之外为空
  • 任何想法如何 gt 这个工作?

非常感谢

4

0 回答 0