简体   繁体   English

从 json 文件读取时,如何返回对象而不是数组内的对象?

[英]How can I return objects instead of objects inside an array when reading from json files?

I'm reading some json files from a directory and serving them as an endpoint.我正在从目录中读取一些 json 文件并将它们作为端点提供服务。 I have the below code:我有以下代码:

 fs.readdir(dirPath, function (err, filesPath) {
            if (err) throw err;
            filesPath = filesPath.map(function(filePath){ 
                return dirPath + filePath;
            });
            async.map(filesPath, function(filePath, cb){ 
                fs.readFile(filePath, 'utf8', cb);
            }, function(err, results) {
                res.send(results);
            });
        });

This returning something like this:这将返回如下内容:

[         
  {
    "Country1":{
       "countryTEST":"US",
       "FindLocale":{
          "Test":{
             "Test":false,
             "Test":""
          },
          "Test":{
             "Test":false,
             "Test":"value"
          }
       },
        "payment":[
          "CREDIT_CARD",
          "NOT_CREDIT"
       ],
       "test":"1234",
       "phoneNumb":[
          ""
       ]
    },

    "Country2":{
       "countryTEST":"US",
       "FindLocale":{
          "Test":{
             "Test":false,
             "Test":""
          },
          "Test":{
             "Test":false,
             "Test":"value"
          }
       },
        "payment":[
          "CREDIT_CARD",
          "NOT_CREDIT"
       ],
       "test":"1234"
       "phoneNumb":[
          ""
       ]
    }
  }
]

However, I want the returned response to just look like this (without the array wrapping the objects)但是,我希望返回的响应看起来像这样(没有包装对象的数组)

 {
    "Country1":{
       "countryTEST":"US",
       "FindLocale":{
          "Test":{
             "Test":false,
             "Test":""
          },
          "Test":{
             "Test":false,
             "Test":"value"
          }
       },
        "payment":[
          "CREDIT_CARD",
          "NOT_CREDIT"
       ],
       "test":"1234"
       "phoneNumb":[
          ""
       ]
    },

    "Country2":{
       "countryTEST":"US",
       "FindLocale":{
          "Test":{
             "Test":false,
             "Test":""
          },
          "Test":{
             "Test":false,
             "Test":"value"
          }
       },
        "payment":[
          "CREDIT_CARD",
          "NOT_CREDIT"
       ],
       "test":"1234"
       "phoneNumb":[
          ""
       ]
    }
  }

I have tried doing something like res.send(JSON.stringify(results));我试过做类似res.send(JSON.stringify(results));事情res.send(JSON.stringify(results)); and res.send(JSON.parse(results));res.send(JSON.parse(results)); . . But this didn't for work me to get my desired output.但这并没有让我得到我想要的输出。

Please kindly suggest how I can get the output I need.请建议我如何获得我需要的输出。 Any suggestion is welcome.欢迎任何建议。 Thank you!谢谢!

To convert an array to object:将数组转换为对象:

 let arr = [ { "country1": { "capital": "some capital", "currency": "$" } }, { "country2": { "capital": "some capital", "currency": "$" } } ]; let obj = Object.assign({}, ...arr); console.log(obj);

请试试这个您需要该数组的第一个元素并进行解析。

res.send(JSON.parse(results[0]));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在D3 Sankey中,如何从对象或数组而不是json或csv文件加载? - In D3 Sankey, how can I load the from objects or arrays instead of either json or csv files? 如何根据每个 object 中的数组从 object 数组中返回某些对象? - How can I return certain objects from object array based on an array inside each object? 如何将对象数组转换为JSON(并返回)? - How can I convert an array of objects into JSON (and return it)? 我想 JSON 解析对象内的对象数组 - 正文中的文章,我该怎么做? - I want to JSON Parse an array of objects inside an object - articles from the body, how can I do that? Typescript:如何从具有更改属性名称的数组中的 JSON 个对象中提取值? - Typescript: How can I extract the value from JSON objects inside an array with changing property names? 如何获取数组中的对象? - How can i take the objects inside in the array? 从json文件导入时,如何修复未定义对象数组的变量? - How can I fix variables of an array of objects being undefined when being imported from a json file? 如何通过对象内的参数对对象数组进行排序? - How can I sort a array of objects by a parameter inside an objects? 如果我使用传播语法,如何完全返回内部嵌套对象的数组? - How can I completely return the array with nested array of objects inside if I am using spread syntax? 如何获取对象数组而不是数组数组 - How can I get array of objects instead of array of array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM