-
• #2
Can you please post the JSON in its entirety?
-
• #3
This is JSON as parsed by node.js on a desktop:
{ week_number: 50, utc_offset: '-05:00', utc_datetime: '2019-12-15T18:10:33.811491+00:00', unixtime: 1576433433, timezone: 'America/Toronto', raw_offset: -18000, dst_until: null, dst_offset: 0, dst_from: null, dst: false, day_of_year: 349, day_of_week: 0, datetime: '2019-12-15T13:10:33.811491-05:00', client_ip: '174.114.161.151', abbreviation: 'EST' }
The actual text string comes from https://worldtimeapi.org/api/timezone/America/New_York
-
• #4
res.on('data',
is triggered as soon as the first data arrives, not when all data is there. That causes the error, if you replace the lineconsole.log(JSON.parse(data).datetime);
withconsole.log('###');
you'll see that `res.on('data',
is triggered multiple times.
As such you're trying to parse an incomplete JSON string, and the error is normal.
Edit: And it's pure luck that it works in node.js. -
• #5
If you know the protocol between JSON strings - such as cr/lf, cr, lf, you can write something like is done for GPS where the (non-JSON) sentences come in line by line. With every new arrival of a data fragment you look for the separation (ends and begins) and process when one found... see GPS module source at https://www.espruino.com/modules/GPS.js
-
• #6
Check this snippet to set the time:
http://forum.espruino.com/conversations/280894/#12755183
Find more at the "Tips and Tricks" page
-
• #7
Just to add the fix for what you want is just the second example here: https://www.espruino.com/Internet#client
require("http").get("http://www.espruino.com", function(res) { var contents = ""; res.on('data', function(data) { contents += data; }); res.on('close', function() { console.log(contents); }); });
basically get all the data, then parse in one go when the socket is closed
-
• #8
Thanks.
I use the following code on a desktop computer as well as on an ESP8266 (2v04).
node.js on the desktop could parse the output just fine :
ESP8266 shows the following:
1 Attachment