由于一些奇怪的原因,我似乎无法遍历我的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);
});
});
Swift Code
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
}
]
}
}
答案 0 :(得分:1)
您需要返回httpResponse.data而不是httpResponse.text,因为您的响应对象中的content-type是application / json