SwiftyJson 从 JSON 的复杂结构中获取 json 的值
SwiftyJson get value of json from complicated structure of JSON
这是我的 json.file:
[
{
"date_range": "2016-11-01-2016-12-31",
"order_status_id": 3,
"jobs": [
{
"date": "2016-11-14",
"job": [
{
"id": 143608,
"pickup_worker_id": null,
"drop_off_worker_id": 57
}
]
}
]
}
{
"date_range": "2016-11-01-2016-12-31",
"order_status_id": 2,
"jobs": [
{
"date": "2016-11-16",
"job": [
{
"id": 143238,
"pickup_worker_id": null,
"drop_off_worker_id": 12
},
{
"id": 13218,
"pickup_worker_id": null,
"drop_off_worker_id": 42
}
]
},
{
"date": "2016-11-19",
"job": [
{
"id": 141238,
"pickup_worker_id": null,
"drop_off_worker_id": 12
}
]
}
]
}
]
这是我 swiftyjson
的代码:
Alamofire.request(Constants.web_api+api_get_orders, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: Constants.headers ).responseJSON { (responseData) -> Void in
if((responseData.result.value) != nil) {
let swiftyJsonVar = JSON(responseData.result.value!)
print("All Data JSON \(swiftyJsonVar)")
print("date range1\(swiftyJsonVar["date_range"])")
print("date range2\(swiftyJsonVar["date_range"].stringValue)")
print("jobs1 \(swiftyJsonVar["jobs"].arrayObject)")
print("jobs2 \(swiftyJsonVar["jobs"].array)")
print("jobs3 \(swiftyJsonVar["jobs"])")
print("jobs date \(swiftyJsonVar["jobs"]["date"].stringValue)")
print("jobs date \(swiftyJsonVar["jobs"]["job"]["drop_off_worker_id"].stringValue)")
}
输出,除所有数据 JSON (swiftyJsonVar
) 外,均为 null 或 nil。我怎样才能得到 date_range
, drop_off_worker_id
的价值?我真的希望有人能帮助我。花了很多时间解决还是解决不了
您的 JSON 响应是 Array
而不是 Dictionary
,因此您需要访问其第一个对象以获取所需的详细信息。
if let dateRange = swiftyJsonVar[0]["date_range"].string {
print(dateRange)
}
if let worker_id = swiftyJsonVar[0]["jobs"][0]["job"][0]["drop_off_worker_id"].int {
print(worker_id)
}
编辑: 如果你的根中有多个对象而不是在 for 循环中获取所有 dateRange
和 worker_id
。
for subJson in swiftyJsonVar.array {
if let dateRange = subJson["date_range"].string {
print(dateRange)
}
for jobsJson in subJson["jobs"].array {
for jobJson in jobsJson["job"].array {
if let worker_id = jobJson["drop_off_worker_id"].int {
print(worker_id)
}
}
}
}
请尝试,通过引用数组中元素的索引。
Alamofire.request(Constants.web_api+api_get_orders, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: Constants.headers)
.responseJSON { (responseData) -> Void in
if((responseData.result.value) != nil) {
let swiftyJsonVar = JSON(responseData.result.value!)
print("All Data JSON \(swiftyJsonVar)")
print("date range1\(swiftyJsonVar[0]["date_range"])")
print("date range2\(swiftyJsonVar[0]["date_range"].stringValue)")
print("jobs1 \(swiftyJsonVar[0]["jobs"].arrayObject)")
print("jobs2 \(swiftyJsonVar[0]["jobs"].array)")
print("jobs3 \(swiftyJsonVar[0]["jobs"])")
print("jobs date \(swiftyJsonVar[0]["jobs"]["date"].stringValue)")
print("jobs date \(swiftyJsonVar[0]["jobs"]["job"]["drop_off_worker_id"].stringValue)")
}
这是我的 json.file:
[
{
"date_range": "2016-11-01-2016-12-31",
"order_status_id": 3,
"jobs": [
{
"date": "2016-11-14",
"job": [
{
"id": 143608,
"pickup_worker_id": null,
"drop_off_worker_id": 57
}
]
}
]
}
{
"date_range": "2016-11-01-2016-12-31",
"order_status_id": 2,
"jobs": [
{
"date": "2016-11-16",
"job": [
{
"id": 143238,
"pickup_worker_id": null,
"drop_off_worker_id": 12
},
{
"id": 13218,
"pickup_worker_id": null,
"drop_off_worker_id": 42
}
]
},
{
"date": "2016-11-19",
"job": [
{
"id": 141238,
"pickup_worker_id": null,
"drop_off_worker_id": 12
}
]
}
]
}
]
这是我 swiftyjson
的代码:
Alamofire.request(Constants.web_api+api_get_orders, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: Constants.headers ).responseJSON { (responseData) -> Void in
if((responseData.result.value) != nil) {
let swiftyJsonVar = JSON(responseData.result.value!)
print("All Data JSON \(swiftyJsonVar)")
print("date range1\(swiftyJsonVar["date_range"])")
print("date range2\(swiftyJsonVar["date_range"].stringValue)")
print("jobs1 \(swiftyJsonVar["jobs"].arrayObject)")
print("jobs2 \(swiftyJsonVar["jobs"].array)")
print("jobs3 \(swiftyJsonVar["jobs"])")
print("jobs date \(swiftyJsonVar["jobs"]["date"].stringValue)")
print("jobs date \(swiftyJsonVar["jobs"]["job"]["drop_off_worker_id"].stringValue)")
}
输出,除所有数据 JSON (swiftyJsonVar
) 外,均为 null 或 nil。我怎样才能得到 date_range
, drop_off_worker_id
的价值?我真的希望有人能帮助我。花了很多时间解决还是解决不了
您的 JSON 响应是 Array
而不是 Dictionary
,因此您需要访问其第一个对象以获取所需的详细信息。
if let dateRange = swiftyJsonVar[0]["date_range"].string {
print(dateRange)
}
if let worker_id = swiftyJsonVar[0]["jobs"][0]["job"][0]["drop_off_worker_id"].int {
print(worker_id)
}
编辑: 如果你的根中有多个对象而不是在 for 循环中获取所有 dateRange
和 worker_id
。
for subJson in swiftyJsonVar.array {
if let dateRange = subJson["date_range"].string {
print(dateRange)
}
for jobsJson in subJson["jobs"].array {
for jobJson in jobsJson["job"].array {
if let worker_id = jobJson["drop_off_worker_id"].int {
print(worker_id)
}
}
}
}
请尝试,通过引用数组中元素的索引。
Alamofire.request(Constants.web_api+api_get_orders, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: Constants.headers)
.responseJSON { (responseData) -> Void in
if((responseData.result.value) != nil) {
let swiftyJsonVar = JSON(responseData.result.value!)
print("All Data JSON \(swiftyJsonVar)")
print("date range1\(swiftyJsonVar[0]["date_range"])")
print("date range2\(swiftyJsonVar[0]["date_range"].stringValue)")
print("jobs1 \(swiftyJsonVar[0]["jobs"].arrayObject)")
print("jobs2 \(swiftyJsonVar[0]["jobs"].array)")
print("jobs3 \(swiftyJsonVar[0]["jobs"])")
print("jobs date \(swiftyJsonVar[0]["jobs"]["date"].stringValue)")
print("jobs date \(swiftyJsonVar[0]["jobs"]["job"]["drop_off_worker_id"].stringValue)")
}