简体   繁体   English

Node.js - 解析简单的JSON对象并访问键和值

[英]Node.js - Parse simple JSON object and access keys and values

I am new to Node and struggling to access a simple JSON object. 我是Node的新手并且正在努力访问一个简单的JSON对象。 My request.body will have JSON content similar to the following: 我的request.body将具有类似于以下内容的JSON内容:

{
    "store_config": [
        {
            "name": "hello",
            "name2": "world"
        }
    ]
}

The "store_config" value will always be present, however the keys and values within could be anything. “store_config”值将始终存在,但其中的键和值可以是任何值。

How can I iterate through the keys and values to access each? 如何迭代键和值来访问每个键? I would also like to process each in an asynchronous manner. 我还想以异步方式处理每个。

Appreciate any thoughts or direction. 欣赏任何想法或方向。


UPDATE UPDATE

console.log(typeof(request.body));

Returns: Object 返回: Object

parsedBody = JSON.parse(request.body);

returns: 收益:

SyntaxError: Unexpected token o
    at Object.parse (native)

UPDATE 2 - Further debug: 更新2 - 进一步调试:

When I try to iterate through the array, there is only a single value: 当我尝试遍历数组时,只有一个值:

request.body.store_config.forEach(function(item, index) {
  console.log(index);
  console.log(request.body.store_config[index]);

});

returns: 收益:

0
{ name: 'hello', name2: 'world' }

If request.body is already being parsed as JSON, you can just access the data as a JavaScript object; 如果request.body已经被解析为JSON,那么您只需将数据作为JavaScript对象访问; for example, 例如,

request.body.store_config

Otherwise, you'll need to parse it with JSON.parse : 否则,您需要使用JSON.parse解析它:

parsedBody = JSON.parse(request.body);

Since store_config is an array, you can iterate over it: 由于store_config是一个数组,您可以迭代它:

request.body.store_config.forEach(function(item, index) {
  // `item` is the next item in the array
  // `index` is the numeric position in the array, e.g. `array[index] == item`
});

If you need to do asynchronous processing on each item in the array, and need to know when it's done, I recommend you take a look at an async helper library like async --in particular, async.forEach may be useful for you : 如果你需要对数组中的每个项进行异步处理,并且需要知道它什么时候完成,我建议你看看像async这样的异步助手库 - 特别是async.forEach对你有用

async.forEach(request.body.store_config, function(item, callback) {
  someAsyncFunction(item, callback);
}, function(err){
  // if any of the async callbacks produced an error, err would equal that error
});

I talk a little bit about asynchronous processing with the async library in this screencast . 在这个截屏视频中讨论async库的异步处理。

Something like this: 像这样的东西:

config = JSON.parse(jsonString);
for(var i = 0; i < config.store_config.length; ++i) {
   for(key in config.store_config[i]) {
      yourAsyncFunction.call(this, key, config.store_config[i][key]);
   }
}

To convert this sting to an actual object, use JSON.parse . 要将此sting转换为实际对象,请使用JSON.parse You can iterate though Javascript objects like you would with an array. 您可以像使用数组一样迭代Javascript对象。

config = JSON.parse(string).store_config[0]
foreach (var key in config) {
    value = config[key]
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM