JSON 解析 swift 3 提取数据
JSON Parsing with swift 3 to extract data
以下是我使用
得到的 JSON 结果
let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSDictionary
但我无法从数据中提取标题、author_data["name"]和摘要。
我想将数据存储在局部变量中。我该怎么做?
JSON 结果:
{
"current_page" = 1;
data = (
{
"author_data" = (
{
id = "kiyosaki_robert_t";
name = "Kiyosaki, Robert T.";
}
);
"awards_text" = "";
"book_id" = "rich_dads_the_business_school_a01";
"dewey_decimal" = "";
"dewey_normal" = 0;
"edition_info" = "Hardcover; 2008-08-30";
isbn10 = 8186775811;
isbn13 = 9788186775813;
language = "";
"lcc_number" = "";
"marc_enc_level" = "~";
notes = "";
"physical_description_text" = "6.1\"x9.1\"x0.4\"; 0.4 lb";
"publisher_id" = "manjul_publishing_house_pvt_lt";
"publisher_name" = "Manjul Publishing House Pvt Ltd";
"publisher_text" = "Manjul Publishing House Pvt Ltd";
"subject_ids" = (
"business_investing_general"
);
summary = "Presents eight hidden values of a network marketing business. This book is suitable for those associated with network marketing.";
title = "Rich Dad's the Business School";
"title_latin" = "Rich Dad's the Business School";
"title_long" = "";
"urls_text" = "";
}
);
"index_searched" = isbn;
"page_count" = 1;
"result_count" = 1;
}
如果您使用 Swift 本机词典而不是 NSDictionary
,您可以轻松地从 Dictionary
访问数据,而且对于本机词典,也无需指定 mutableContainers
选项.现在你的 data
是 Dictionary
的数组,author_data
是 data
数组的第一个 Dictionary
中的数组,你可以这样得到它的名字。
if let jsonDic = try? JSONSerialization.jsonObject(with: urlContent, options: []) as? [String:Any] {
if let dataArray = jsonDic["data"] as? [[String:Any]], let firstObj = dataArray.first {
if let summary = firstObj["summary"] as? String,
let author_data = firstObj["author_data"] as? [[String:Any]],
let authorObj = author_data.first,
let name = authorObj["name"] {
print(summary)
print(name)
}
}
}
以下是我使用
得到的 JSON 结果let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSDictionary
但我无法从数据中提取标题、author_data["name"]和摘要。
我想将数据存储在局部变量中。我该怎么做?
JSON 结果:
{
"current_page" = 1;
data = (
{
"author_data" = (
{
id = "kiyosaki_robert_t";
name = "Kiyosaki, Robert T.";
}
);
"awards_text" = "";
"book_id" = "rich_dads_the_business_school_a01";
"dewey_decimal" = "";
"dewey_normal" = 0;
"edition_info" = "Hardcover; 2008-08-30";
isbn10 = 8186775811;
isbn13 = 9788186775813;
language = "";
"lcc_number" = "";
"marc_enc_level" = "~";
notes = "";
"physical_description_text" = "6.1\"x9.1\"x0.4\"; 0.4 lb";
"publisher_id" = "manjul_publishing_house_pvt_lt";
"publisher_name" = "Manjul Publishing House Pvt Ltd";
"publisher_text" = "Manjul Publishing House Pvt Ltd";
"subject_ids" = (
"business_investing_general"
);
summary = "Presents eight hidden values of a network marketing business. This book is suitable for those associated with network marketing.";
title = "Rich Dad's the Business School";
"title_latin" = "Rich Dad's the Business School";
"title_long" = "";
"urls_text" = "";
}
);
"index_searched" = isbn;
"page_count" = 1;
"result_count" = 1;
}
如果您使用 Swift 本机词典而不是 NSDictionary
,您可以轻松地从 Dictionary
访问数据,而且对于本机词典,也无需指定 mutableContainers
选项.现在你的 data
是 Dictionary
的数组,author_data
是 data
数组的第一个 Dictionary
中的数组,你可以这样得到它的名字。
if let jsonDic = try? JSONSerialization.jsonObject(with: urlContent, options: []) as? [String:Any] {
if let dataArray = jsonDic["data"] as? [[String:Any]], let firstObj = dataArray.first {
if let summary = firstObj["summary"] as? String,
let author_data = firstObj["author_data"] as? [[String:Any]],
let authorObj = author_data.first,
let name = authorObj["name"] {
print(summary)
print(name)
}
}
}