我使用JsonConverter VBA模块将数据从Excel导出为标准JSON格式。 JSON输出类似于以下内容:-
[{
"Manufacturer": "Ford",
"Code": 5551234,
"Model": "Escort"
"Status": "Available"
},
{
"Manufacturer": "Ford",
"Code": 5551335,
"Model": "Mondeo"
"Status": "Out of stock"
},
{
"Manufacturer": "Ford",
"Code": 5551240,
"Model": "Fiesta"
"Status": "Available"
},
{
"Manufacturer": "BMW",
"Code": 5552567,
"Model": "M1"
"Status": "Available"
},
{
"Manufacturer": "BMW",
"Code": 5552328,
"Model": "M2"
"Status": "Available"
},
{
"Manufacturer": "BMW",
"Code": 5552573,
"Model": "M3"
"Status": "Out of stock"
}
]
我正在使用的VBA代码如下:-
Sub Excel2JSON()
Dim excelRange As Range
Dim jsonItems As New Collection
Dim jsonDictionary As New Dictionary
Dim jsonFileObject As New FileSystemObject
Dim jsonFileExport As TextStream
Dim i As Long
Dim cell As Variant
Set excelRange = Cells(1, 1).CurrentRegion
For i = 2 To excelRange.Rows.Count
jsonDictionary("Manufacturer") = Cells(i, 1)
jsonDictionary("Code") = Cells(i, 2)
jsonDictionary("Model") = Cells(i, 3)
jsonDictionary("Status") = Cells(i, 4)
jsonItems.Add jsonDictionary
Set jsonDictionary = Nothing
Next i
Set jsonFileExport = jsonFileObject.CreateTextFile(".../cardata.json", True)
jsonFileExport.WriteLine (JsonConverter.ConvertToJson(jsonItems, Whitespace:=3))
End Sub
然后我使用以下脚本将该JSON导入HTML表:-
$(document).ready(function() {
$.ajax({
url: "cardata.json",
method: "GET",
dataType: "json",
success: function(data) {
var $tbody = $("table#data tbody");
$.each(data, function(i, data) {
var $tr = $("<tr></tr>");
$tr.appendTo($tbody);
var $td = $("<td></td>");
$td.html(data.Manufacturer)
.appendTo($tr);
$td = $("<td></td>");
$td.html(data.Code)
.appendTo($tr);
$td = $("<td></td>");
$td.html(data.Model)
.appendTo($tr);
$td = $("<td></td>");
$td.html(data.Status)
.appendTo($tr);
});
},
});
});
问题/问题有两个方面。我想将JSON导入2个不同的HTML表中,而不是一个表中(一个表用于Ford,另一个表用于BMW等)。我知道下面的JSON将可用于2个不同的表,但是我无法修改VBA代码来创建一个如下所示的JSON:-
{
"Ford": [{
"Code": 5551234,
"Model": "Escort"
"Status": "Available"
},
{
"Code": 5551335,
"Model": "Mondeo",
"Status": "Out of stock"
},
{
"Code": 5551240,
"Model": "Fiesta",
"Status": "Available"
}
],
"BMW": [{
"Code": 5552567,
"Model": "M1",
"Status": "Available"
},
{
"Code": 5552328,
"Model": "M2",
"Status": "Available"
},
{
"Code": 5552573,
"Model": "M3",
"Status": "Out of stock"
}
]
}
另一种方法是保持JSON不变,但更改脚本将其导入到2个不同的HTML表中的方式。
答案 0 :(得分:1)
以下,我已使用jQuery以所需的格式更改了您的JSON。
$(document).ready(function(){
$.ajax({
url: "cardata.json",
method: "GET",
dataType: "json",
success: function(data){
var manufacturers = {};
data.forEach(function(element) {
if(!manufacturers[element.Manufacturer])
manufacturers[element.Manufacturer] = new Array();
var obj = {};
obj.Code = element.Code;
obj.Model = element.Model;
obj.Status = element.Status;
manufacturers[element.Manufacturer].push(obj);
});
console.log(manufacturers); //As object
console.log(JSON.stringify(manufacturers)); // In JSON
}
});
});
答案 1 :(得分:0)
使用Ash提供的代码,现在可以正确填充表格。很可能我已经将数据附加了很长时间,但是可以正常工作。
$(document).ready(function() {
$.ajax({
url: "cardata.txt",
method: "GET",
dataType: "json",
success: function(data) {
var vendors = {};
data.forEach(function(element) {
if (!manufacturers[element.Manufacturer])
manufacturers[element.Manufacturer] = new Array();
var obj = {};
obj.Code = element.Code;
obj.Model = element.Model;
obj.Status = element.Status;
manufacturers[element.Manufacturer].push(obj);
});
var $tbody = $("table#BMW tbody");
$.each(manufacturers.BMW, function(i, data) {
var $tr = $("<tr></tr>");
$tr.appendTo($tbody);
var $td = $("<td></td>");
$td.html(data.Code)
.appendTo($tr);
$td = $("<td></td>");
$td.html(data.Model)
.appendTo($tr);
$td = $("<td></td>");
$td.html(data.Status)
.appendTo($tr);
});
var $tbody = $("table#Ford tbody");
$.each(manufacturers.Ford, function(i, data) {
var $tr = $("<tr></tr>");
$tr.appendTo($tbody);
var $td = $("<td></td>");
$td.html(data.Code)
.appendTo($tr);
$td = $("<td></td>");
$td.html(data.Model)
.appendTo($tr);
$td = $("<td></td>");
$td.html(data.Status)
.appendTo($tr);
});
console.log(manufacturers); //As object
console.log(JSON.stringify(manufacturers)); // In JSON
}
});
});