kye
kye

Reputation: 2246

parse.com httpresponse return json not text

For some strange reason i cant seem to loop through the result returned by my cloudcode function.I think its because it's returning a text in json format not actual json? How can i parse the httpResponse value into a json to be used later?
I've tried using

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) 
}

Sample 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
}
]

}
}

Upvotes: 1

Views: 339

Answers (1)

Mo Nazemi
Mo Nazemi

Reputation: 2717

You need to return httpResponse.data instead of httpResponse.text, given that the content-type in your response object is application/json

Upvotes: 1

Related Questions