以预期格式打印JSON数组

以预期格式打印JSON数组

本文介绍了以预期格式打印JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是从表中获取详细信息的代码.

This is the code to get the details from my table.

 function listshops(callback)
{
 var array=[3,4];/*shopId*/
  async.each(array,function(dat,callback){

     async.parallel([

         function(callback){
            client.connection.query('select * from shop where shopId=?',dat,function(err,data1){
            callback(null,data1);

           });
        },

         function (callback) {
            client.connection.query('select * from image where shopId=?',dat,function(err,data2){
            callback(null,data2);

           });
        }
     ],
     function(err,data)
     {
        var result = data.reduce(function(prev, curr) { /*merging the array*/
          return prev.concat(curr);
        });
        console.log(result);
     });
 });

}

我得到了这样的输出: http://i.stack.imgur.com /NVUAu.png

I got an output like this:: http://i.stack.imgur.com/NVUAu.png

我想以以下格式打印结果:

i want to print my result in the below format:

{
"shops": [
    {
        "shopId": "3",
        "shopName": "1",
        "address": "abc",
        "contactNumber":"1234"
        "images": [
            {
                "imageId": "1",
                "shopId": "3",
                "imageUrl": "aaa",
            },
            {
                "imageId": "2",
                "shopId": "3",
                "imageUrl": "bbb",
            },
        ]
    },
    {
        "shopId": "4",
        "shopName": "2",
        "address": "bbb",
        "contactNumber":"1234"
        "images": [
            {
                "imageId": "3",
                "shopId": "4",
                "imageUrl": "ccc",
            },
            {
                "imageId": "4",
                "shopId": "4",
                "imageUrl": "ddd",
            },
        ]
    },
]

我得到了值,但是在获取值时有些困惑.

I got the values but some confusions in fetching the values.

推荐答案

您在此处有两个嵌套的异步任务,分别为paralleleach.

You have two nested async tasks here, parallel and each.

Parallel负责获取一家商店的商店信息和图像,并使用包含您的任务结果的两个元素数组调用最终回调.

Parallel takes care of getting the shop information and the images for one shop and calls the final callback with a two element array that has the results for your tasks.

这是您对并行的最终回调:

This is your final callback for parallel:

    function(err,data)
         {
            var result = data.reduce(function(prev, curr) { /*merging the array*/
              return prev.concat(curr);
            });
            console.log(result);
         });

data应该是一个两个元素的数组,其中元素0是商店,元素1是图像.您只需将它们串联在一起.如果要以所需的格式显示图像,则应执行data[0].images = data[1]将图像密钥添加到商店.

data should be a two element array, where element 0 is the shop, and element 1 are the images. You just concatenate these together. If you want your images in the desired format, you should do data[0].images = data[1] to add the images key to your shop.

到目前为止,这仅适用于一家商店.然后是外部each循环.您当前没有给它做最后的回调,也没有对结果做任何事情.

That so far is for one shop. Then there is the outer each loop. You are currently not giving this a final callback and not doing anything with the result.

类似这样的东西:

     function listshops(callback)
{
 var array=[3,4];/*shopId*/
  async.each(array,function(dat,eachCallback){

     async.parallel([

         function(parallelCallback){
            client.connection.query('select * from shop where shopId=?',dat,function(err,data1){
            parallelCallback(null,data1);

           });
        },

         function (parallelCallback) {
            client.connection.query('select * from image where shopId=?',dat,function(err,data2){
            parallelCallback(null,data2);

           });
        }
     ],
     // this is the final callback for parallel
     function(err,parallelResult)
     {
        // construct one shop
        var shop = parallelResult[0];
        shop.image = parallelResult[1];
        // pass the shop info back as a result to each
        eachCallBack(shop);
     });
 },
 // this is the final callback for each
 function(err, eachResult) {
    // eachResult is an array with the shops returned from parallel callback
    var result = {
        shops: eachResult
    }

    return result
 }
 );

}

我无法对此进行测试,因此不要将其视为确切的解决方案,而只是一种解释.我重命名了您的变量以更好地理解,您不必在代码中这样做.要记住的关键是,您在这里有两个嵌套任务,例如一个嵌套循环,还必须在两个级别上处理结果.

I couldn't test this, so don't consider it an exact solution, but an explanation. I renamed your variables for better understandability, you don't have to do that in your code. The key thing to keep in mind is that you have two nested tasks here, like a nested loop and you also have to deal with the results on two levels.

这篇关于以预期格式打印JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 09:17