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 に答える