0

我们在 SpookyJS 中有一个函数,它返回一个JSON object只包含一个巨大arraystringsGET 方法在nodeJS. 我们希望它返回 aJSON object并将arrayofstrings转换为arrayofJSON objects

注意:我们最终希望能够在这些对象AngularJS发生变化时使用这些对象。

这是功能:

res.send({reviewCount: reviews.length, reviews: reviews});

这是它返回的内容:

{
    "reviewCount": 96,
    "reviews": [
        "\nSean Steinman\nreviewed 2 weeks ago\n Fantastic Service\n",
        "\nRyan Lundell\nreviewed in the last week\n Ask for Scott!\n• • •\n"
    ]
}

我们想要返回的内容:

{
    "reviewCount": 96,
    "reviews": [
        {name: "Sean Steinman", date: "reviewed 2 weeks ago", review: "Fantastic Service"},
        {name: "Ryan Lundell", date: "reviewed in the last week", review:"Ask for Scott!"}
    ]
}

我们尝试过使用JSON.stringify(),但它只是将所有内容转换为一个 JSON 对象:

res.send({reviewCount: reviews.length, reviews: JSON.stringify(reviews)});
4

1 回答 1

0

我最终弄清楚了。这是我所做的:

spooky.run(function(){
       var reviewObj = {
          reviewCount: reviews.length,
          reviews: []
        }
        reviews.forEach(function(line){   
          //turn the string into an array           
          var review = line.split('\n');
          });
          //add the array elements to the object
          reviewObj.reviews.push({name: review[0], date: review[1], reviewContent: review[2]});

        }); 

       this.echo('about to emit');
       this.emit("gotReviews", reviewObj);
    });
于 2014-07-18T04:35:18.820 回答