设法从URL生成正确的JSON数据。但是如何使用JSON数据创建2个单独的数组以产生以下输出?
PrefectureCode = ["1", "2", ....]
PrefectureName = ["A", "B", ....]
***结果JSON ****
{
"message" : null,
"result" : [
{
"prefCode" : 1,
"prefName" : "北海道"
},
{
"prefCode" : 2,
"prefName" : "青森県"
},
{
"prefCode" : 3,
"prefName" : "岩手県"
},
{
"prefCode" : 4,
"prefName" : "宮城県"
},
{
"prefCode" : 5,
"prefName" : "秋田県"
},
{
"prefCode" : 6,
"prefName" : "山形県"
},
{
"prefCode" : 7,
"prefName" : "福島県"
},
{
"prefCode" : 8,
"prefName" : "茨城県"
},
{
"prefCode" : 9,
"prefName" : "栃木県"
},
{
"prefCode" : 10,
"prefName" : "群馬県"
},
{
"prefCode" : 11,
"prefName" : "埼玉県"
},
{
"prefCode" : 12,
"prefName" : "千葉県"
},
{
"prefCode" : 13,
"prefName" : "東京都"
},
{
"prefCode" : 14,
"prefName" : "神奈川県"
},
{
"prefCode" : 15,
"prefName" : "新潟県"
},
{
"prefCode" : 16,
"prefName" : "富山県"
},
{
"prefCode" : 17,
"prefName" : "石川県"
},
{
"prefCode" : 18,
"prefName" : "福井県"
},
{
"prefCode" : 19,
"prefName" : "山梨県"
},
{
"prefCode" : 20,
"prefName" : "長野県"
},
{
"prefCode" : 21,
"prefName" : "岐阜県"
},
{
"prefCode" : 22,
"prefName" : "静岡県"
},
{
"prefCode" : 23,
"prefName" : "愛知県"
},
{
"prefCode" : 24,
"prefName" : "三重県"
},
{
"prefCode" : 25,
"prefName" : "滋賀県"
},
{
"prefCode" : 26,
"prefName" : "京都府"
},
{
"prefCode" : 27,
"prefName" : "大阪府"
},
{
"prefCode" : 28,
"prefName" : "兵庫県"
},
{
"prefCode" : 29,
"prefName" : "奈良県"
},
{
"prefCode" : 30,
"prefName" : "和歌山県"
},
{
"prefCode" : 31,
"prefName" : "鳥取県"
},
{
"prefCode" : 32,
"prefName" : "島根県"
},
{
"prefCode" : 33,
"prefName" : "岡山県"
},
{
"prefCode" : 34,
"prefName" : "広島県"
},
{
"prefCode" : 35,
"prefName" : "山口県"
},
{
"prefCode" : 36,
"prefName" : "徳島県"
},
{
"prefCode" : 37,
"prefName" : "香川県"
},
{
"prefCode" : 38,
"prefName" : "愛媛県"
},
{
"prefCode" : 39,
"prefName" : "高知県"
},
{
"prefCode" : 40,
"prefName" : "福岡県"
},
{
"prefCode" : 41,
"prefName" : "佐賀県"
},
{
"prefCode" : 42,
"prefName" : "長崎県"
},
{
"prefCode" : 43,
"prefName" : "熊本県"
},
{
"prefCode" : 44,
"prefName" : "大分県"
},
{
"prefCode" : 45,
"prefName" : "宮崎県"
},
{
"prefCode" : 46,
"prefName" : "鹿児島県"
},
{
"prefCode" : 47,
"prefName" : "沖縄県"
}
]
}
答案 0 :(得分:0)
您可以使用array#reduce
并将prefCode
和prefName
的数据推入相应的数组中,并对结果进行解构。
let response = { "message" : null, "result" : [ { "prefCode" : 1, "prefName" : "北海道" }, { "prefCode" : 2, "prefName" : "青森県" }, { "prefCode" : 3, "prefName" : "岩手県" }, { "prefCode" : 4, "prefName" : "宮城県" }, { "prefCode" : 5, "prefName" : "秋田県" }, { "prefCode" : 6, "prefName" : "山形県" }, { "prefCode" : 7, "prefName" : "福島県" }, { "prefCode" : 8, "prefName" : "茨城県" }, { "prefCode" : 9, "prefName" : "栃木県" }, { "prefCode" : 10, "prefName" : "群馬県" }, { "prefCode" : 11, "prefName" : "埼玉県" }, { "prefCode" : 12, "prefName" : "千葉県" }, { "prefCode" : 13, "prefName" : "東京都" }, { "prefCode" : 14, "prefName" : "神奈川県" }, { "prefCode" : 15, "prefName" : "新潟県" }, { "prefCode" : 16, "prefName" : "富山県" }, { "prefCode" : 17, "prefName" : "石川県" }, { "prefCode" : 18, "prefName" : "福井県" }, { "prefCode" : 19, "prefName" : "山梨県" }, { "prefCode" : 20, "prefName" : "長野県" }, { "prefCode" : 21, "prefName" : "岐阜県" }, { "prefCode" : 22, "prefName" : "静岡県" }, { "prefCode" : 23, "prefName" : "愛知県" }, { "prefCode" : 24, "prefName" : "三重県" }, { "prefCode" : 25, "prefName" : "滋賀県" }, { "prefCode" : 26, "prefName" : "京都府" }, { "prefCode" : 27, "prefName" : "大阪府" }, { "prefCode" : 28, "prefName" : "兵庫県" }, { "prefCode" : 29, "prefName" : "奈良県" }, { "prefCode" : 30, "prefName" : "和歌山県" }, { "prefCode" : 31, "prefName" : "鳥取県" }, { "prefCode" : 32, "prefName" : "島根県" }, { "prefCode" : 33, "prefName" : "岡山県" }, { "prefCode" : 34, "prefName" : "広島県" }, { "prefCode" : 35, "prefName" : "山口県" }, { "prefCode" : 36, "prefName" : "徳島県" }, { "prefCode" : 37, "prefName" : "香川県" }, { "prefCode" : 38, "prefName" : "愛媛県" }, { "prefCode" : 39, "prefName" : "高知県" }, { "prefCode" : 40, "prefName" : "福岡県" }, { "prefCode" : 41, "prefName" : "佐賀県" }, { "prefCode" : 42, "prefName" : "長崎県" }, { "prefCode" : 43, "prefName" : "熊本県" }, { "prefCode" : 44, "prefName" : "大分県" }, { "prefCode" : 45, "prefName" : "宮崎県" }, { "prefCode" : 46, "prefName" : "鹿児島県" }, { "prefCode" : 47, "prefName" : "沖縄県" } ] },
{PrefectureCode, PrefectureName} = response.result.reduce((r,{prefCode, prefName}) => {
r.PrefectureCode.push(prefCode);
r.PrefectureName.push(prefName);
return r;
},{PrefectureCode: [], PrefectureName: []});
console.log(PrefectureCode, PrefectureName);
答案 1 :(得分:0)
以下是我用来尝试解析JSON数据的代码,我陷入了“执行”过程中:
导入UIKit 导入SwiftyJSON 导入Alamofire
struct prefectureInfo:可编码{ 让prefCode:Int吗? 让prefName:String吗? }
ViewController类:UIViewController {
var prefectureinfo = [prefectureInfo]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let baseURL = "https://opendata.resas-portal.go.jp/api/v1/prefectures?"
let ID = "4GMF0uUh7T9UHFvcp8xq6r4WnGnBA6arqJpuoPIS"
let param = ["Content-Type" : "application/json;charset=UTF-8"]
let header: HTTPHeaders = ["X-API-KEY" : ID]
getRESASdata(url: baseURL, parameters: param, header: header)
}
func getRESASdata(url: String, parameters: Parameters, header: HTTPHeaders) {
Alamofire.request(url, method: .get, parameters: parameters, headers: header).responseJSON {
response in
if response.result.isSuccess {
print("Success! Got data from RESAS")
let dataJSON : JSON = JSON(response.result.value!)
//print(dataJSON)
self.updateData(json: dataJSON)
do {
let decoder = JSONDecoder()
self.prefectureinfo = try decoder.decode([prefectureInfo].self, from: dataJSON)
for prefecture in self.prefectureinfo{
print(prefecture.prefName!)
}
}catch let err {
print(err)
}
}
else {
print("Error \(String(describing: response.result.error))")
}
}
}
//MARK: - JSON Parsing
func updateData(json : JSON) {
let tempResult = json["result"]
print(tempResult)
//****** Parsing to array *********
}
}