简体   繁体   中英

(object Object) returning from JSON request in Node

I have another simple question about Node. I'm trying to make a simple http request to the Open Courseware search API ( http://www.ocwsearch.com/api ), but I'm running into some problems actually parsing the JSON... which normally hasn't been a problem. The request returns a string that has escaped characters and slashes, and so I've run a replace and an unescape on the response string, but this ends up returning something like '[object Object]'. Right now, all I really want to do is be able to SEE what's finally being returned, so I can tell whether or not I can finally parse it as valid JSON. Unfortunately, this is not working either. I've read a couple of similar threads on stack overflow, but I'm still unable to get it to work.

What I've tried:

  • iterating over the object being passed, because I thought it'd be something with key value pairs, and running over it, logging it each time. However, this prints it out as a string:

[

o

b

... all the way to the end (])

Given x as the returned formatted object in question,

None of these seem to work though, they're all returning me [object Object].

I've even tried it in jsfiddle: http://jsfiddle.net/S47QL/2/ I'm replacing console.log with alert, and it seems to be re

My code:

var request = require('request');$
request('http://www.ocwsearch.com/api/v1/search.json?q=' + skill +     '&contact=http%3a%2f%2fwww.ocwsearch.com%2fabout/',$
  function(error, response, body){$
  if(!error && response.statusCode == 200){~$
      console.log(response.toString().replace(/\\\//g, "/"));$
        var x = response.toString().replace(/\\\//g, "/");$
        console.log(x)$
        console.log(x.keys());$
  }$  
});$

Right now, all I really want to do is be able to SEE what's finally being returned, so I can tell whether or not I can finally parse it as valid JSON.

So then make the request in a browser window (using Chrome you can see the actual results being returned in the inspector tabs - press F12).

Example: http://www.ocwsearch.com/api/v1/search.json?q=ios&contact=http%3a%2f%2fwww.ocwsearch.com%2fabout/

And according to http://jsonlint.com/ this is valid JSON being returned. Are you sure you're not trying to be too smart for your own good?

You can't write the response to string, because it's an object. You need the data returned, but I think the body field does what you want. Have you tried just printing body ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM