出于某些奇怪的原因,我似乎无法遍历我的 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/