我想更改数组的结构以生成稍微不同的JSON文件。我尝试将import UIKit
enum Section: Int {
case toDo
case done
}
class ItemListDataProvider: NSObject, UITableViewDataSource, UITableViewDelegate {
var itemManager: ItemManager?
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard let itemManager = itemManager else{return 0}
guard let itemSection = Section(rawValue: section)else{ fatalError() }
let numberOfRows: Int
switch itemSection {
case .toDo:
numberOfRows = itemManager.toDoCount
case .done:
numberOfRows = itemManager.doneCount
}
return numberOfRows
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell", for: indexPath) as! ItemCell
guard let itemManager = itemManager else { fatalError() }
guard let section = Section(rawValue: indexPath.section) else { fatalError() }
let item: ToDoItem
switch section {
case .toDo:
item = itemManager.item(at: indexPath.row)
case .done:
item = itemManager.doneItem(at: indexPath.row)
}
cell.configCell(with: item)
return cell
}
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
}
放入额外的数组中,但它不起作用。
在JSON 'category'
中应该是aditional []:
'category'
而不是:
"categories": [{
"category": [{
"label": "N.12\/02"
}, {
"label": "Pn.13\/02"
}, {
"label": "Wt.14\/02"
}, {
"label": "\u015ar.15\/02"
}, {
"label": "Cz.16\/02"
}, {
"label": "Pt.17\/02"
}, {
"label": "So.18\/02"
}]
}],
我使用此代码创建数组而不是JSON:
"categories": {
"category": [{
"label": "N.12\/02"
}, {
"label": "Pn.13\/02"
}, {
"label": "Wt.14\/02"
}, {
"label": "\u015ar.15\/02"
}, {
"label": "Cz.16\/02"
}, {
"label": "Pt.17\/02"
}, {
"label": "So.18\/02"
}]
},
此刻我有这个输出:
$arrData = array(
"chart" => array(
"caption" => "Number of visitors last week",
"subCaption" => "Bakersfield Central vs Los Angeles Topanga",
"captionFontSize" => "14",
"subcaptionFontSize" => "14",
"subcaptionFontBold" => "0",
"paletteColors" => "#0075c2,#1aaf5d,#FF5733,#33B5FF",
"bgcolor" => "#ffffff",
"showBorder" => "0",
"showShadow" => "0",
"showCanvasBorder" => "0",
"usePlotGradientColor" => "0",
"legendBorderAlpha" => "0",
"legendShadow" => "0",
"showAxisLines" => "0",
"showAlternateHGridColor" => "0",
"divlineThickness" => "1",
"divLineDashed" => "1",
"divLineDashLen" => "1",
"xAxisName" => "Day",
"showValues" => "0"
)
);
$actualCategory = array( $rowData[0][1], $rowData[0][2], $rowData[0][3], $rowData[0][4], $rowData[0][5], $rowData[0][6], $rowData[0][7]);
$a = array(
array( $rowData[2][1], $rowData[2][2], $rowData[2][3], $rowData[2][4], $rowData[2][5], $rowData[2][6], $rowData[2][7]),
array( $rowData[5][1], $rowData[5][2], $rowData[5][3], $rowData[5][4], $rowData[5][5], $rowData[5][6], $rowData[5][7]),
array( $rowData[8][1], $rowData[8][2], $rowData[8][3], $rowData[8][4], $rowData[8][5], $rowData[8][6], $rowData[8][7]),
array( $rowData[11][1], $rowData[11][2], $rowData[11][3], $rowData[11][4], $rowData[11][5], $rowData[11][6], $rowData[11][7])
);
$arrData['categories'] = array( 'category' => array());
foreach ($actualCategory as $value) {
array_push($arrData['categories']['category'],
array(
'label' => $value
)
);
}
$arrData['dataset'] = array();
$q = 1;
for($z = 0;$z <= 3; $z++){
$x[$z] = array('seriesname' => $rowData[$q][0], 'data' => array());
foreach ($a[$z] as $value) {
array_push($x[$z]['data'],
array(
'value' => $value
)
);
}
$q = $q + 3;
}
$arrData['dataset'] = $x;
$jsonEncodedData = json_encode($arrData);
我需要的JSON文件:
{"chart":{"caption":"Number of visitors last week","subCaption":"Bakersfield Central vs Los Angeles Topanga","captionFontSize":"14","subcaptionFontSize":"14","subcaptionFontBold":"0","paletteColors":"#0075c2,#1aaf5d,#FF5733,#33B5FF","bgcolor":"#ffffff","showBorder":"0","showShadow":"0","showCanvasBorder":"0","usePlotGradientColor":"0","legendBorderAlpha":"0","legendShadow":"0","showAxisLines":"0","showAlternateHGridColor":"0","divlineThickness":"1","divLineDashed":"1","divLineDashLen":"1","xAxisName":"Day","showValues":"0"},"categories":{"category":[{"label":"N.12\/02"},{"label":"Pn.13\/02"},{"label":"Wt.14\/02"},{"label":"\u015ar.15\/02"},{"label":"Cz.16\/02"},{"label":"Pt.17\/02"},{"label":"So.18\/02"}]},"dataset":[{"seriesname":"IRDN","data":[{"value":"142.59"},{"value":"174.88"},{"value":"176.97"},{"value":"182.48"},{"value":"160.15"},{"value":"160.72"},{"value":"165.47"}]},{"seriesname":"SIRDN","data":[{"value":"148.81"},{"value":"197.29"},{"value":"202.27"},{"value":"211.93"},{"value":"177.87"},{"value":"179.37"},{"value":"177.69"}]},{"seriesname":"IRDN24","data":[{"value":"140.31"},{"value":"174.50"},{"value":"180.38"},{"value":"187.70"},{"value":"161.91"},{"value":"161.62"},{"value":"160.98"}]},{"seriesname":"IRDN 8.22","data":[{"value":"147.33"},{"value":"197.02"},{"value":"202.21"},{"value":"211.28"},{"value":"178.11"},{"value":"179.32"},{"value":"176.31"}]}]}
我的阵列转储:
{"chart":{"caption":"Number of visitors last week","subCaption":"Bakersfield Central vs Los Angeles Topanga","captionFontSize":"14","subcaptionFontSize":"14","subcaptionFontBold":"0","paletteColors":"#0075c2,#1aaf5d,#FF5733,#33B5FF","bgcolor":"#ffffff","showBorder":"0","showShadow":"0","showCanvasBorder":"0","usePlotGradientColor":"0","legendBorderAlpha":"0","legendShadow":"0","showAxisLines":"0","showAlternateHGridColor":"0","divlineThickness":"1","divLineDashed":"1","divLineDashLen":"1","xAxisName":"Day","showValues":"0"},"categories":[{"category":[{"label":"N.12\/02"},{"label":"Pn.13\/02"},{"label":"Wt.14\/02"},{"label":"\u015ar.15\/02"},{"label":"Cz.16\/02"},{"label":"Pt.17\/02"},{"label":"So.18\/02"}]}],"dataset":[{"seriesname":"IRDN","data":[{"value":"142.59"},{"value":"174.88"},{"value":"176.97"},{"value":"182.48"},{"value":"160.15"},{"value":"160.72"},{"value":"165.47"}]},{"seriesname":"SIRDN","data":[{"value":"148.81"},{"value":"197.29"},{"value":"202.27"},{"value":"211.93"},{"value":"177.87"},{"value":"179.37"},{"value":"177.69"}]},{"seriesname":"IRDN24","data":[{"value":"140.31"},{"value":"174.50"},{"value":"180.38"},{"value":"187.70"},{"value":"161.91"},{"value":"161.62"},{"value":"160.98"}]},{"seriesname":"IRDN 8.22","data":[{"value":"147.33"},{"value":"197.02"},{"value":"202.21"},{"value":"211.28"},{"value":"178.11"},{"value":"179.32"},{"value":"176.31"}]}]}
我的问题是如何更改我的代码呢?
答案 0 :(得分:0)
你应该使用
$arrData['categories'] = array( array ('category' => array()));
而不是
$arrData['categories'] = array( 'category' => array());
它将按您的意愿运作。