0

这是我在 app.js 中的代码

app.get('/all_question', function(req, res){


    var listQuestion = function(err, collection) {
        collection.find().toArray(function(err, results) {
                console.log(results);
            res.render('all_question.html', { layout : false , 'title' : 'Question Bank', 'results' : results });
        });
    }

    var Client = new Db('questionbank', new Server('127.0.0.1', 27017, {}));
    Client.open(function(err, pClient) {
        Client.collection('questions', listQuestion);
        //Client.close();
    });

})

这就是我所说的 all_questions.html:

{"questions":[{{#each results}}{"id":"{{this._id}}","subject":"{{this.subject}}","qType":"{{this.qType}}","score":"{{this.score}}","question":{"name":"{{this.question.name}}","choice":["{{this.question.choice}}"],"answer":["{{this.question.answer}}"]}}{{/each}},]}`

这是结果:

{"questions":[{"id":"50d2de8879f86d9663000002","subject":"General","qType":"1","score":"1","question":{"name":"Earth is the center of the Universe","choice":["False,True"],"answer":["1"]}}{"id":"50d2e29e79f86d9663000003","subject":"General","qType":"2","score":"1","question":{"name":"Which is the biggest continent","choice":["Australia,Asia,Africa"],"answer":["2"]}},]}

由于需要用逗号分隔每条记录,因此我在每条记录的末尾输出一个逗号。但它也适用于最后一条记录。但是,当我在 iOS 中将其用作 Web 服务调用来输入 NSDictionary 时,由于最后一条记录中的逗号,我收到了一个错误。我该如何消除这个?

4

1 回答 1

0
NSString *newString = [oldString stringByReplacingOccurrencesOfString:@"," withString:"" options:0 range:NSMakeRange(source.length - 3, 3)];
于 2012-12-21T10:01:20.033 回答