以下是我使用
获得的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;
}
答案 0 :(得分:0)
如果您使用Swift本地词典而不是Dictionary
也可以使用本地词典轻松访问NSDictionary
中的数据,则无需指定mutableContainers
选项。现在,您的data
是Dictionary
的数组,而author_data
是Dictionary
数组的data
内的数组,您可以像这样得到它的名字。
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)
}
}
}