Alex Bailey
Alex Bailey

Reputation: 1512

Struggling to convert JSON string in Javascript to JSON

So I'm making a hybrid mobile application using angular and I have this string that comes from a $http.jsonp request. The response that I receive I'm trying to turn in to JSON. I've checked the JSON request at https://jsonformatter.curiousconcept.com/ and it seems that the JSON is valid.

The error I receive in the Chrome web console is "Uncaught Syntax Error: Unexpected token :" and I can't for the life of me work out why this would be?

Using the response from the server below, can someone tell me what I'm doing wrong here?

{
   "rss":{
      "version":"2.0",
      "xmlns:content":"http://purl.org/rss/1.0/modules/content/",
      "xmlns:wfw":"http://wellformedweb.org/CommentAPI/",
      "xmlns:dc":"http://purl.org/dc/elements/1.1/",
      "xmlns:atom":"http://www.w3.org/2005/Atom",
      "xmlns:sy":"http://purl.org/rss/1.0/modules/syndication/",
      "xmlns:slash":"http://purl.org/rss/1.0/modules/slash/",
      "channel":{
         "title":"WEBSITE TITLE",
         "link":[
            {
               "href":"https://www.WEBURL.com/feed/",
               "rel":"self",
               "type":"application/rss+xml"
            },
            "https://www.WEBURL.com"
         ],
         "description":"WEB DESCRIPTION",
         "lastBuildDate":"Fri, 25 Sep 2015 16:33:52 +0000",
         "language":"en-US",
         "updatePeriod":"hourly",
         "updateFrequency":"1",
         "generator":"http://wordpress.org/?v=4.3",
         "item":[
            {
               "title":"TITLE",
               "link":"https://www.WEBURL.com/2015/09/25/POST",
               "comments":[
                  "https://www.WEBURL.com/2015/09/25/POST/#comments",
                  "0"
               ],
               "pubDate":"Fri, 25 Sep 2015 16:27:19 +0000",
               "creator":"AUTHOR",
               "category":"CATEGORY",
               "guid":"https://www.WEBURL.com/?p=12345",
               "description":"<p>POST DESCRIPTION</p>\n",
               "encoded":"<p class=\"article-content__figure article-content--image\">POST CONTENT</p>\n",
               "commentRss":"https://www.WEBURL.com/2015/09/25/POST/feed/"
            }
         ]
      }
   }
}

The Javascript goes as:

$http.jsonp("http://jsonburner.herokuapp.com/source?feed=https://www.WEBURL.com/feed/")
            .success(function(data) {
            $scope.debug = data.rss.version
            //$scope.debug = data.rss.version

        })
        .error(function(data) {
            $scope.debug = data
            //$scope.debug = data.rss.version

        });

Thanks

Alex

Upvotes: 0

Views: 71

Answers (2)

Arman
Arman

Reputation: 88

You're probably looking to do this:

$http.get('http://jsonburner.herokuapp.com/source?feed=https://www.WEBURL.com/feed/')
.success(function(result) {
  var data = JSON.parse(result);
  console.log('this is what you\'re looking for: ', data);
  
}).error(function(result) {
  var data = JSON.parse(result);
});

Here you have JSONP explained: Can anyone explain what JSONP is, in layman terms?

Upvotes: 0

Fridjon Gudjohnsen
Fridjon Gudjohnsen

Reputation: 827

I think you are confusing JSON and JSONP (see here for the difference between them).

You probably just want to use $http.get (or what ever verb the server behind the URL expects). See $http docs for details.

Upvotes: 1

Related Questions