将字符串格式化为JSON以供jqGrid使用

时间:2015-06-05 03:21:08

标签: json jqgrid

我从csv获得以下txt。我想将其转换为Json格式。

id: "1", invdate: "2007-10-01", name: "test", note: "note", amount: "200.00", tax: "10.00", total: "210.00"
id: "2", invdate: "2007-10-02", name: "test2", note: "note2", amount: "300.00", tax: "20.00", total: "320.00"

var js = '';

var rows = txt.split('\n');
for (var i = 1; i < rows.length; i++) {
    js += '{' + rows[i] + '},';
}

js = js.slice(0, -1); //remove the last comma

var obj = JSON.parse('{' + js + '}'); //Error Expected '}' on Line1 Column 2

如何正确地将文本格式化为jqgrid接受的json或数组?

colModel: [
                    {label: 'Inv No', name: 'id', width: 75, key: true},
                    {label: 'Date', name: 'invdate', width: 90},
                    {label: 'Client', name: 'name', width: 100},
                    {label: 'Amount', name: 'note', width: 80},
                    {label: 'Tax', name: 'amount', width: 80},
                    {label: 'Total', name: 'tax', width: 80},
                    {label: 'Notes', name: 'total', width: 150}
                ],

0 个答案:

没有答案