消除iOS NSDictionary的json记录中的最后一个逗号

时间:2012-12-21 09:55:51

标签: ios json nsdictionary

这是我在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时,由于最后一条记录中的逗号,我收到错误。我如何消除这个?

1 个答案:

答案 0 :(得分:0)

NSString *newString = [oldString stringByReplacingOccurrencesOfString:@"," withString:"" options:0 range:NSMakeRange(source.length - 3, 3)];