json - parse.com httpresponse 返回 json 而不是文本

标签 json xcode swift parse-platform

出于某些奇怪的原因,我似乎无法遍历我的 cloudcode 函数返回的结果。我认为这是因为它返回的是 json 格式的文本而不是实际的 json?如何将 httpResponse 值解析为 json 以备后用?
我试过使用

response.success(JSON.parse(httpResponse.text)); // and httpResponse.data

Parse.com CloudCode

 Parse.Cloud.define("test", function(request, response) {
        return Parse.Cloud.httpRequest({
            url: 'https://api.spotify.com/v1/search?q=the+eagles&type=artist&limit=1'

        }).then(function(httpResponse) {
            response.success(httpResponse.text);


        }, 
        function (error) {
            response.error("Error: " + error.code + " " + error.message);
        });
    });

快速代码

PFCloud.callFunctionInBackground("test", withParameters: ["" : ""]) { (result: AnyObject?, error: NSError?) in
        let json = JSON(result!); //SwiftyJSON
        print(json["artist"]) //returns null
        //print(json[0])//returns null
        //print(json) 
}

示例 JSON

{
artists: {
href: "https://api.spotify.com/v1/search?query=the+eagles&offset=0&limit=1&type=artist",
items: [
{
genres: [
"country rock",
"mellow gold",
"soft rock"
],
href: "https://api.spotify.com/v1/artists/0ECwFtbIWEVNwjlrfc6xoL",
id: "0ECwFtbIWEVNwjlrfc6xoL",
images: [
{
height: 668,
url: "https://i.scdn.co/image/173519085c3eb8301fbeb744b0b92b6747938ab3",
width: 1000
}
]

}
}

最佳答案

您需要返回 httpResponse.data 而不是 httpResponse.text,因为您的响应对象中的内容类型是 application/json

关于json - parse.com httpresponse 返回 json 而不是文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33182657/

相关文章:

swift - 由于内存问题而终止 - ViewControllers 占用内存

arrays - Swift - 从类中追加数组时遇到问题

ios - 首次使用电子邮件登录后强制更新 Firebase 用户个人资料照片

python - 无法读取非 ASCII 内容

javascript - 将变量按字面意思放入 JSON 中

ios - 使用 MapKit Swift 在两个坐标之间绘制路径

ios - libpaypalmpl.a 正在使用 uniqueidentifier 并且 apple reject 拒绝该应用程序

swift - 为什么并行测试在 Xcode 10 中默认不可用?

json - 使用 jq 通过搜索子键/值对来选择父级

java - 重命名未反射(reflect)在 JSON 输出中的类变量