Reputation: 25
I am trying to build an app. I have no prior Java experience and had initially intended to do this via LAMP yet found out that was not the best solution. So I'm a tad green
I am using nodejs on a windows 8 machine for development. I am pulling data from the ebay api. I am trying to parse the JSON I get back (no clue what I'm doing)
The code that I know is broken:
JSON.parse(body);
The JSON itself:
/**/_cb_findItemsByKeywords({"findCompletedItemsResponse":[{"ack":["Success"],"version": ["1.12.0"],"timesta
mp":["2014-08-17T15:39:18.735Z"],"searchResult":[{"@count":"1","item":[{"itemId": ["231283411176"],"title":["Nine West Te
ched Out Snap on Case for iPhone 5 Shoe Lover Hard Cover CHOP 3OM0z1"],"globalId":["EBAY-US"],"primaryCategory":[{"categ
oryId":["20349"],"categoryName":["Cases, Covers & Skins"]}],"galleryURL":["http:\/ \/thumbs1.ebaystatic.com\/m\/mFGCczOCC
B_D_F8sXXJ1QrA\/140.jpg"],"viewItemURL":["http:\/\/www.ebay.com\/itm\/Nine-West-Teched-Out-Snap-Case-iPhone-5-Shoe-Lover
-Hard-Cover-CHOP-3OM0z1-\/231283411176?pt=US_Cell_Phone_PDA_Cases"],"paymentMethod":["PayPal"],"autoPay":["false"],"post
alCode":["08003"],"location":["Cherry Hill,NJ,USA"],"country":["US"],"shippingInfo":[{"shippingServiceCost":[{"@currency
Id":"USD","__value__":"3.89"}],"shippingType":["Flat"],"shipToLocations":["Worldwide"],"expeditedShipping":["true"],"one
DayShippingAvailable":["true"],"handlingTime":["0"]}],"sellingStatus":[{"currentPrice":[{"@currencyId":"USD","__value__"
:"15.07"}],"convertedCurrentPrice":[{"@currencyId":"USD","__value__":"15.07"}],"sellingState":["EndedWithoutSales"]}],"l
istingInfo":[{"bestOfferEnabled":["false"],"buyItNowAvailable":["false"],"startTime":["2014-07-15T09:58:29.000Z"],"endTi
me":["2014-09-13T09:58:29.000Z"],"listingType":["FixedPrice"],"gift":["false"]}],"returnsAccepted":["true"],"condition":
[{"conditionId":["1500"],"conditionDisplayName":["New other (see details)"]}],"isMultiVariationListing":["false"],"disco
untPriceInfo":[{"originalRetailPrice": [{"@currencyId":"USD","__value__":"19.99"}],"pricingTreatment":["STP"],"soldOnEbay
":["false"],"soldOffEbay":["false"]}],"topRatedListing":["true"]}]}],"paginationOutput":[{"pageNumber":["1"],"entriesPer
Page":["1"],"totalPages":["848492"],"totalEntries":["848492"]}]}]})
The error from nodejs:
SyntaxError: Unexpected token /
at Object.parse (native)
at Request._callback (C:\Users\WolJoshu\Desktop\temp\nodejs\index.js:45:11)
at Request.self.callback (C:\Users\WolJoshu\Desktop\temp\nodejs\node_modules\request\request.js:123:22)
at Request.EventEmitter.emit (events.js:98:17)
at Request.<anonymous> (C:\Users\WolJoshu\Desktop\temp\nodejs\node_modules\request\request.js:1047:14)
at Request.EventEmitter.emit (events.js:117:20)
at IncomingMessage.<anonymous> (C:\Users\WolJoshu\Desktop\temp\nodejs\node_modules\request\request.js:998:12)
at IncomingMessage.EventEmitter.emit (events.js:117:20)
at _stream_readable.js:870:14
at process._tickCallback (node.js:415:13)
The code:
var request = require('request');
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
//This defines what main page to load
app.get('/', function(req, res){
res.sendfile('engine.html');
});
io.on('connection', function(socket){
// Simply lets us know when users connect and disconnect
console.log('a user connected');
socket.on('disconnect', function(){
console.log('user disconnected');
});
//When data comes in do the following
socket.on('incomingdata', function(msg){
//put the data back to the client
io.emit('incomingdata', msg);
//This builds the ebay request
var url = "http://svcs.ebay.com/services/search/FindingService/v1";
url += "?OPERATION-NAME=findCompletedItems";
url += "&SERVICE-VERSION=1.0.0";
url += "&SECURITY-APPNAME=deleted";
url += "&GLOBAL-ID=EBAY-US";
url += "&RESPONSE-DATA-FORMAT=JSON";
url += "&callback=_cb_findItemsByKeywords";
url += "&REST-PAYLOAD";
url += "&keywords=" + msg;
url += "&paginationInput.entriesPerPage=1";
//This puts the request in
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
//Here is some experimental json parcing stuff
JSON.parse(body);
// console.log('Here is the parsed text: ' + sample);
//This echos our search term to the console
console.log('Here is the search term: ' + msg);
//this pumps it back in to the client in raw HTML
io.emit('incomingdata', body );
console.log('incomingdata', body );
}
})
});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});'
Upvotes: 0
Views: 694
Reputation: 475
The error message indicates that the parse is dying on the first character '/'; JSON won't accept comments, just JSON encoding (double quotes, objects and arrays, ISO8601 dates, etc).
I was able to strip out the JSON payload between "findItemsByKeyWords(" and the last closing paren, and send it to http://jsoneditoronline.org/ to verify that it parses, so that's good. but you need to make sure the body only includes valid JSON, or, preprocess to strip out the leading comment and "findBy" call prior to parsing. You probably also want to set your headers to "accept" JSON if that's what you want.
Upvotes: 1