菜鸟教程小白 发表于 2022-12-11 17:00:44

ios - 用于公共(public)休息 API 的 NSData 对象中的 JSON


                                            <p><p>我正在尝试将数据从我的应用程序发送到另一个程序员开发的 Android 应用程序也在使用该 API。我使用 NSJSONSerialization.dataWithJSONObject 将 JSON 转换为 NSData 对象,然后将其附加到 NSURLRequest 但 NSData 对象是 JSON 字符串的十六进制表示。根据另一位开发人员的说法,他的 Android 代码正在以 UTF-8 编码创建和传输 JSON 对象,所以我的问题是如何将 JSON 字符串作为 UTF-8 文本发送,或者使 API 能够尽可能无缝地处理这两个来源?</p>

<p>编辑:我现在使用的代码</p>

<pre><code>func postToServer() {

    let endPoint: String = &#34;http://server.com&#34;
    guard let url = NSURL(string: endPoint) else {
      print(&#34;ERROR: cannot create URL&#34;)
      return
    }
    let urlRequest = NSMutableURLRequest(URL: url)
    urlRequest.HTTPMethod = &#34;POST&#34;
    urlRequest.setValue(&#34;application/json; charset=utf-8&#34;, forHTTPHeaderField: &#34;Content-Type&#34;)

    let loc = self.getLocation()
    var content: = [&#34;action&#34;: &#34;put-point&#34;, &#34;request&#34;: [&#34;rangeKey&#34;: self.id, &#34;lng&#34;: loc.coordinate.longitude, &#34;lat&#34;: loc.coordinate.latitude, &#34;count&#34;: self.count]]

    var data: NSData! = NSData()
    do {
      data = try NSJSONSerialization.dataWithJSONObject(content, options: NSJSONWritingOptions())
      print(data)
    } catch {
      print (&#34;Error&#34;)
    }

    urlRequest.HTTPBody = data

    let config = NSURLSessionConfiguration.defaultSessionConfiguration()
    let session = NSURLSession(configuration: config)


    let task = session.dataTaskWithRequest(urlRequest, completionHandler:{ data, response, error in
      guard error == nil else {
            print(&#34;ERROR: Cannot call Get on endpoint&#34;)
            print(error)
            return
      }

      guard let responseData = data else {
            print(&#34;ERROR: Did not receive any data&#34;)
            return
      }

      print(&#34;DATA: \(data)&#34;)

    })
    task.resume()
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>你可以这样做</p>

<pre><code>let jsonObj = [...]
var data = NSData()

do {
    data = try NSJSONSerialization.dataWithJSONObject(jsonObj, options: .PrettyPrinted)
    let dataString = NSString(data: data, encoding: NSUTF8StringEncoding)!
} catch {
    print(&#34;error:\(error)&#34;)
}
</code></pre>

<p>*在 Swift 2 和 Xcode 7.3.1 上试过</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 用于公共(public)休息 API 的 NSData 对象中的 JSON,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/38471265/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/38471265/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 用于公共(public)休息 API 的 NSData 对象中的 JSON