【问题标题】:JSON File append ObjectsJSON 文件追加对象
【发布时间】:2017-10-17 00:10:27
【问题描述】:

我正在尝试迭代几十个链接,并希望通过递增每个索引并获取下一个 pageNumber 来保存所有结果

//dependencies
const jsonfile = require('jsonfile')
const _        = require('lodash')
const utf8     = require('utf8')
const fs       = require('fs')

//config
const file = './data/home.json'
const unique = [ 
                 { suggest: 'København kommune', count: '2577' },
                 { suggest: 'København K, 1000', count: '14' },
                 { suggest: 'København K, 1100', count: '36' } 
               ];

// begin iterations
_.forEach(unique, (data) => {

  var totalPages = Math.ceil(data.count / 99); // get total page numbers for each suggestion

  for ( var i=0; i < totalPages; i++){

    /**
     * Map Cities from our array of unique suggestions
     * Fetch promises for each city
     * @param i {i++} the number of pages we can iterate
     * @param fetch {data.suggest} individual fetch calls
     */

    fetch(`https://home.dk/umbraco/backoffice/home-api/Search?SortType=&SortOrder=&CurrentPageNumber=${i}&SearchResultsPerPage=99&q=${utf8.encode(data.suggest)}&EjendomstypeV1=&EjendomstypeRH=&EjendomstypeEL=&EjendomstypeVL=&EjendomstypeAA=&EjendomstypePL=&EjendomstypeFH=&EjendomstypeLO=&EjendomstypeHG=&EjendomstypeFG=&EjendomstypeNL=&Forretningnr=&ProjectNodeId=&PriceMin=&PriceMax=&EjerudgiftPrMdrMin=&EjerudgiftPrMdrMax=&BoligydelsePrMdrMin=&BoligydelsePrMdrMax=&BoligstoerrelseMin=&BoligstoerrelseMax=&GrundstoerrelseMin=&GrundstoerrelseMax=&VaerelserMin=&VaerelserMax=&Energimaerker%5B%5D=null&ByggaarMin=&ByggaarMax=&EtageMin=&EtageMax=&PlanMin=&PlanMax=&Aabenthus=&Foraeldrekoebsegnet=&Projektsalg=&BoligKanLejes=&MapLngSW=&MapLatSW=&MapLngNE=&MapLatNE=&UserLng=&UserLat=&SearchType=0`)
    .then(response => response.json())
    .then(validate => { 

          validate.searchResults.map(all=> {

                  var result = {
                      longitude:  data.lng, 
                      latitude:   data.lat
                  }


                   jsonfile.writeFileSync(file, result, {spaces: 2, flag: 'a'})
                   //    fs.appendFileSync(file, ',\n'); // my stupid way of adding commas at the end of each object....


          }) // validate.searchResults.map 

    }) // then(validate)
  } // for( i < totalPages )

}); // _.forEach(unique, (data) => {

问题在于输出不会创建尾随逗号,也不会包含在数组中。我正在使用标志:'a' 方法在每个循环上附加对象....请帮助

// bad output
{
  "longitude": 12.5893247949521,
  "latitude": 55.6768760704466
}
{
  "longitude": 12.5821935238857,
  "latitude": 55.6778938800944
}
{
  "longitude": 12.5905159440853,
  "latitude": 55.6803210250784
}

//prefered output

[ // nest all objects inside an array
    {
    "longitude": 12.5893247949521,
    "latitude": 55.6768760704466
    }, // <--- add comma - ,,,,,
    {
    "longitude": 12.5821935238857,
    "latitude": 55.6778938800944
    }, // <--- add comma - ,,,,,
    {
    "longitude": 12.5905159440853,
    "latitude": 55.6803210250784
    }
] // close array

我尝试将所有内容推送到一个数组,但它达到了一个点,其中推送了 60.000 多个结果,并且在尝试保存结果时它有点冻结系统。

我想通过使用标志将每次迭代附加到文件中:“a”方法或管道方法...希望有人有更好的方法将此类数据保存到 JSON 文件!

非常感谢您!

【问题讨论】:

    标签: arrays json node.js object for-loop


    【解决方案1】:

    如果要在文件中存储数组,则先创建数组:

    var result = [];
    _.forEach(unique, (data) => {
      result.push({
          longitude:  data.lng, 
          latitude:   data.lat
      });
    });
    jsonfile.writeFileSync(file, result, {spaces: 2, flag: 'a'})
    

    根据unique 是什么,有更优雅的方式。例如。如果是数组:

    var result = unique.map(data => ({
      longitude:  data.lng, 
      latitude:   data.lat
    }));
    

    【讨论】:

    • 谢谢菲利克斯,很抱歉没有提供更好的例子......希望你仍然可以提供帮助
    猜你喜欢
    • 2020-07-30
    • 2022-10-03
    • 1970-01-01
    • 2012-06-09
    • 2017-10-18
    • 1970-01-01
    • 2013-08-15
    • 1970-01-01
    • 2020-07-15
    相关资源
    最近更新 更多