Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
339 views
in Technique[技术] by (71.8m points)

javascript - How can I JSON stringify the response of a website and save it to a file

How can I JSON stringify the response of a website and use FS to save the JSON stringified response to a file?

This is my code but it didn't convert to JSON:

var request = require("request")
var fs = require('fs');
var url = "http://rubycp.me/"
var http = require('http');


var file = fs.createWriteStream("file.json");
var url = http.get("http://rubycp.me/", function(response) {
    JSON.stringify(response.pipe(file));
});

I simply want to convert the html of the page into JSON and store that into a json file.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

response is a stream object that does not hold any data. You first need to collect all the data of the stream using the data event. If all data is collect the end event is triggered, at that even you can stringify your collected data and write it to the file.

const fs = require('fs')
const http = require('http')

var url = 'http://rubycp.me/'

var file = fs.createWriteStream('file.json')

http.get(url, response => {
  // list that will hold all received chunks
  var result = []
  response
    .on('data', chunk => result.push(chunk)) // add received chunk to list
    .on('end', () => {
      file.write(JSON.stringify(Buffer.concat(result).toString())) // when all chunks are received concat, stringify and write it to the file
    })
})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...