我正在使用你可能不熟悉的tableToJson将表转换为JSON(如果你不知道,我已经在这里包含了代码)。很简单。输出完全符合预期,看起来像:
reader=[
readerType=UCIFastReader
file=$DataDir$/Train.txt
randomize=None
features=[
dim=4
start=0
]
labels=[
dim=2
start=4
labelDim=10
labelMappingFile=$DataDir$/labelsmap.txt
]
]
但是,我需要扩展它以获取每个{"data":[
{"Time":"6:30am - 8:30am","Monday":"","Tuesday":"Maths","Wednesday":"","Thursday":"","Friday":""},
{"Time":"11:15am - 12:45pm","Monday":"Maths","Tuesday":"","Wednesday":"English","Thursday":"","Friday":""},
{"Time":"3:00pm - 5:00pm","Monday":"","Tuesday":"","Wednesday":"English","Thursday":"","Friday":""},
{"Time":"5:00pm - 7:00pm","Monday":"","Tuesday":"Science","Wednesday":"","Thursday":"","Friday":""},
{"Time":"7:00pm - 9:00pm","Monday":"","Tuesday":"Science","Wednesday":"","Thursday":"","Friday":""}]
}
的#id,以便JSON看起来像:(假设表格HTML为<tr>
等。)
<tr id="session 1"></tr>
供参考tableToJson的代码如下:
{
"data":{
"session 1":{"Time":"6:30am - 8:30am","Monday":"","Tuesday":"sdfsdf","Wednesday":"","Thursday":"","Friday":""},
"session 2":{"Time":"11:15am - 12:45pm","Monday":"sdfsdfsdf","Tuesday":"","Wednesday":"","Thursday":"","Friday":""},
"session 3":{"Time":"3:00pm - 5:00pm","Monday":"","Tuesday":"","Wednesday":"","Thursday":"","Friday":""},
"session 4":{"Time":"5:00pm - 7:00pm","Monday":"","Tuesday":"","Wednesday":"","Thursday":"","Friday":""},
"session 5":{"Time":"7:00pm - 9:00pm","Monday":"","Tuesday":"","Wednesday":"","Thursday":"","Friday":""}
}
}
我可能遗漏了一些非常简单的东西,但老实说却看不出怎么做。任何帮助将非常感激。提前谢谢大家!
答案 0 :(得分:0)
请重新考虑为什么需要更改它。这已经在JSON数组中提供了有效的有序内容(可以通过foo.data[0]
,foo.data[1]
等访问,也可以使用for(var i; i<foo.data.length;i++){}
进行迭代)。
你想要的东西会给你一些没有保证顺序的东西(对象中的属性顺序是无关紧要在JSON中)并反对流程。
修改:愚蠢的API 。
如果必须符合某些晦涩难看的标准,请在创建后编辑数据。您不能指望$.fn.tableToJSON
不符合JSON&amp; javascript标准......
var foo = // output from $.fn.tableToJSON
var out = {data:{}}; //declare an *object* instead of an array
for(var i = 0; i < foo.data.length; i++){
out.data['Session ' + (i + 1)] = foo.data[i];
}
//debug output
console.log(JSON.stringify(out))