如何使用消息中的数百个键转换此JSON响应,以便将其分配给变量类型public void actionPerformed(ActionEvent evt) {
if (evt.getSource().equals(addButton)) {
if (source.getSelectedValue() != null) {
String str = (String) source.getSelectedValue();
if(str != null) {
destModel.addElement(str);
dest.setSelectedIndex(0);
sourceModel.removeElement(str);
source.setSelectedIndex(0);
}
}
}
}
JSON
adminConfimSignUp
尝试
解析JSON之后,我无法将[AnyHashable: Any]
分配给接受{
"content": {
"message": {
"greet": "Hello world",
"error": "There is an error",
"thanks": "Thank you",
...
}
}
}
的变量。我试图将jsonData
的类型更改为[AnyHashable: Any]
,但是它不符合message
。
[AnyHashable: Any]
答案 0 :(得分:0)
你们不能同时拥有。
要么想用Decodable
解析JSON,要么必须将testVariable
声明为
private var testVariable : TestCase?
或者您想将testVariable
保留为[AnyHashable: Any]
,然后删除结构并使用传统的JSONSerialization
解码JSON
//print(s)
struct TestCase: Decodable {
let content: Content
}
struct Content: Decodable {
let message: [String: String]
}
// ViewController.swift
private var testVariable = [AnyHashable:Any]()
private func loadJson(filename: String) {
if let url = Bundle.main.url(forResource: filename, withExtension: "json") {
do {
let data = try Data(contentsOf: url)
testVariable = try JSONSerialization.jsonObject(with: data) as? [AnyHashable: Any] ?? [:]
} catch {
print("JsonError: ", error)
}
}
}