我希望在数组中获得capital0的所有值。请帮忙。
{
"status": true,
"code": "200",
"message": "List of States!",
"states_list": [
{
"state_id": "1",
"state_name": "Andhra Pradesh",
"options": {
"capital0": "Panaji",
"capital2": "Amaravati",
"capital1": "Bhubaneswar"
},
"state_map": "http://192.168.1.1/AtoZ/upload/states/andhra pradesh.png"
},
{
"state_id": "2",
"state_name": "Arunachal Pradesh",
"options": {
"capital0": "Jaipur",
"capital2": "Itanagar",
"capital1": "Chennai"
},
"state_map": "http://192.168.1.1/AtoZ/upload/states/arunachal pradesh.png"
},
{
"state_id": "3",
"state_name": "Assam",
"options": {
"capital0": "Agartala",
"capital2": "Dispur",
"capital1": "Amaravati"
},
"state_map": "http://192.168.1.1/AtoZ/upload/states/assam.png"
},
{
"state_id": "4",
"state_name": "Bihar",
"options": {
"capital0": "Agartala",
"capital2": "Patna",
"capital1": "Amaravati"
},
"state_map": "http://192.168.1.1/AtoZ/upload/states/bihar.png"
},
{
"state_id": "5",
"state_name": "Chhattisgarh",
"options": {
"capital0": "Dispur",
"capital2": "Naya Raipur",
"capital1": "Kohima"
},
"state_map": "http://192.168.1.1/AtoZ/upload/states/chhattisgarh.png"
},
{
"state_id": "6",
"state_name": "Goa",
"options": {
"capital0": "Dehradun",
"capital2": "Panaji",
"capital1": "Chennai"
},
"state_map": "http://192.168.1.1/AtoZ/upload/states/goa.png"
},
{
"state_id": "7",
"state_name": "Gujarat",
"options": {
"capital0": "Mumbai",
"capital2": "Gandhinagar",
"capital1": "Chennai"
},
"state_map": "http://192.168.1.1/AtoZ/upload/states/gujrat.png"
},
{
"state_id": "8",
"state_name": "Haryana",
"options": {
"capital0": "Mumbai",
"capital2": "Chandigarh",
"capital1": "Chennai"
},
"state_map": "http://192.168.1.1/AtoZ/upload/states/haryana.png"
},
{
"state_id": "9",
"state_name": "Himachal Pradesh",
"options": {
"capital0": "Naya Raipur",
"capital2": "Shimla",
"capital1": "Mumbai"
},
"state_map": "http://192.168.1.1/AtoZ/upload/states/himachal pradesh.png"
},
{
"state_id": "11",
"state_name": "Jammu and Kashmir",
"options": {
"capital0": "Jaipur",
"capital2": "Srinagar ",
"capital1": "Chennai"
},
"state_map": "http://192.168.1.1/AtoZ/upload/states/jammu & kashmir.png"
},
{
"state_id": "12",
"state_name": "Jharkhand",
"options": {
"capital0": "Chennai",
"capital2": "Ranchi",
"capital1": "Chandigarh"
},
"state_map": "http://192.168.1.1/AtoZ/upload/states/jharkand.png"
},
{
"state_id": "13",
"state_name": "Karnataka",
"options": {
"capital0": "Chennai",
"capital2": "Bangalore",
"capital1": "Chandigarh"
},
"state_map": "http://192.168.1.1/AtoZ/upload/states/karnataka.png"
},
{
"state_id": "14",
"state_name": "Kerala",
"options": {
"capital0": "Srinagar ",
"capital2": "Thiruvananthapuram",
"capital1": "Bhubaneswar"
},
"state_map": "http://192.168.1.1/AtoZ/upload/states/kerala.png"
},
{
"state_id": "15",
"state_name": "Madhya Pradesh",
"options": {
"capital0": "Naya Raipur",
"capital2": "Bhopal",
"capital1": "Bhubaneswar"
},
"state_map": "http://192.168.1.1/AtoZ/upload/states/madhya pradesh.png"
},
{
"state_id": "16",
"state_name": "Maharashtra",
"options": {
"capital0": "Kolkata",
"capital2": "Mumbai",
"capital1": "Jaipur"
},
"state_map": "http://192.168.1.1/AtoZ/upload/states/maharastra.png"
},
{
"state_id": "17",
"state_name": "Manipur",
"options": {
"capital0": "Bhopal",
"capital2": "Imphal",
"capital1": "Chennai"
},
"state_map": "http://192.168.1.1/AtoZ/upload/states/manipur.png"
},
{
"state_id": "18",
"state_name": "Meghalaya",
"options": {
"capital0": "Chennai",
"capital2": "Shillong",
"capital1": "Chandigarh"
},
"state_map": "http://192.168.1.1/AtoZ/upload/states/meghalaya.png"
},
{
"state_id": "19",
"state_name": "Mizoram",
"options": {
"capital0": "Bhopal",
"capital2": "Aizawl",
"capital1": "Chennai"
},
"state_map": "http://192.168.1.1/AtoZ/upload/states/mizoram.png"
},
{
"state_id": "20",
"state_name": "Nagaland",
"options": {
"capital0": "Thiruvananthapuram",
"capital2": "Kohima",
"capital1": "Lucknow"
},
"state_map": "http://192.168.1.1/AtoZ/upload/states/nagaland.png"
},
{
"state_id": "21",
"state_name": "Odisha",
"options": {
"capital0": "Srinagar ",
"capital2": "Bhubaneswar",
"capital1": "Kohima"
},
"state_map": "http://192.168.1.1/AtoZ/upload/states/odisha.png"
},
{
"state_id": "22",
"state_name": "Punjab",
"options": {
"capital0": "Srinagar ",
"capital2": "Chandigarh",
"capital1": "Kohima"
},
"state_map": "http://192.168.1.1/AtoZ/upload/states/punjab.png"
},
{
"state_id": "23",
"state_name": "Rajasthan",
"options": {
"capital0": "Chennai",
"capital2": "Jaipur",
"capital1": "Bhubaneswar"
},
"state_map": "http://192.168.1.1/AtoZ/upload/states/rajasthan.png"
},
{
"state_id": "24",
"state_name": "Sikkim",
"options": {
"capital0": "Srinagar ",
"capital2": "Gangtok",
"capital1": "Kohima"
},
"state_map": "http://192.168.1.1/AtoZ/upload/states/sikkim.png"
},
{
"state_id": "25",
"state_name": "Tamil Nadu",
"options": {
"capital0": "Shimla",
"capital2": "Chennai",
"capital1": "Srinagar "
},
"state_map": "http://192.168.1.1/AtoZ/upload/states/tamil nadu.png"
},
{
"state_id": "26",
"state_name": "Telangana",
"options": {
"capital0": "Gangtok",
"capital2": "Hyderabad",
"capital1": "Bhubaneswar"
},
"state_map": "http://192.168.1.1/AtoZ/upload/states/telangana.png"
},
{
"state_id": "27",
"state_name": "Tripura",
"options": {
"capital0": "Bhopal",
"capital2": "Agartala",
"capital1": "Gangtok"
},
"state_map": "http://192.168.1.1/AtoZ/upload/states/tripura.png"
},
{
"state_id": "28",
"state_name": "Uttar Pradesh",
"options": {
"capital0": "Mumbai",
"capital2": "Lucknow",
"capital1": "Jaipur"
},
"state_map": "http://192.168.1.1/AtoZ/upload/states/uttarakhand.png"
},
{
"state_id": "29",
"state_name": "Uttarakhand",
"options": {
"capital0": "Kolkata",
"capital2": "Dehradun",
"capital1": "Chandigarh"
},
"state_map": "http://192.168.1.1/AtoZ/upload/states/uttar pradesh.png"
},
{
"state_id": "30",
"state_name": "West Bengal",
"options": {
"capital0": "Dehradun",
"capital2": "Kolkata",
"capital1": "Chandigarh"
},
"state_map": "http://192.168.1.1/AtoZ/upload/states/west bengal.png"
}
]
}
答案 0 :(得分:1)
使用Alomafire Object mapper。
1.根据您获得的JSON响应创建模型类。以上链接有例子。它应该与JSON表示相同
YourModel :
import ObjectMapper
class YourModel: Mappable {
var status: Bool?
var code: String?
var message: String ?
var stages: [Stage]?
required init?(map: Map){
}
func mapping(map: Map) {
status <- map["status"]
code <- map["code"]
message <- map["message"]
stages <- map["states_list"]
}
}
class Stage: Mappable {
var state_id: String?
var state_name: String?
var options: [Option]?
var state_map: String?
required init?(map: Map){
}
func mapping(map: Map) {
state_id <- map["state_id"]
state_name <- map["state_name"]
options <- map["options"]
state_map <- map["state_map"]
}
class Option: Mappable {
var capital0: String?
var capital1: String?
var capital2: String?
required init?(map: Map){
}
func mapping(map: Map) {
capital0 <- map["capital0"]
capital1 <- map["capital1"]
capital2 <- map["capital2"]
}
}
调用Alomafire
API:
Alamofire.request(URL).responseObject { (response: DataResponse<YourModel>) in
let modelResponse = response.result.value
// your code
}
答案 1 :(得分:1)
您需要解析json字符串,如下所示
// Your json string, using it directly for demo purpose
let str = """
{"status":true,"code":"200","message":"List of States!","states_list":[{"state_id":"1","state_name":"Andhra Pradesh","options":{"capital0":"Panaji","capital2":"Amaravati","capital1":"Bhubaneswar"},"state_map":"http://192.168.1.1/AtoZ/upload/states/andhra pradesh.png"},{"state_id":"2","state_name":"Arunachal Pradesh","options":{"capital0":"Jaipur","capital2":"Itanagar","capital1":"Chennai"},"state_map":"http://192.168.1.1/AtoZ/upload/states/arunachal pradesh.png"},{"state_id":"3","state_name":"Assam","options":{"capital0":"Agartala","capital2":"Dispur","capital1":"Amaravati"},"state_map":"http://192.168.1.1/AtoZ/upload/states/assam.png"},{"state_id":"4","state_name":"Bihar","options":{"capital0":"Agartala","capital2":"Patna","capital1":"Amaravati"},"state_map":"http://192.168.1.1/AtoZ/upload/states/bihar.png"},{"state_id":"5","state_name":"Chhattisgarh","options":{"capital0":"Dispur","capital2":"Naya Raipur","capital1":"Kohima"},"state_map":"http://192.168.1.1/AtoZ/upload/states/chhattisgarh.png"},{"state_id":"6","state_name":"Goa","options":{"capital0":"Dehradun","capital2":"Panaji","capital1":"Chennai"},"state_map":"http://192.168.1.1/AtoZ/upload/states/goa.png"},{"state_id":"7","state_name":"Gujarat","options":{"capital0":"Mumbai","capital2":"Gandhinagar","capital1":"Chennai"},"state_map":"http://192.168.1.1/AtoZ/upload/states/gujrat.png"},{"state_id":"8","state_name":"Haryana","options":{"capital0":"Mumbai","capital2":"Chandigarh","capital1":"Chennai"},"state_map":"http://192.168.1.1/AtoZ/upload/states/haryana.png"},{"state_id":"9","state_name":"Himachal Pradesh","options":{"capital0":"Naya Raipur","capital2":"Shimla","capital1":"Mumbai"},"state_map":"http://192.168.1.1/AtoZ/upload/states/himachal pradesh.png"},{"state_id":"11","state_name":"Jammu and Kashmir","options":{"capital0":"Jaipur","capital2":"Srinagar ","capital1":"Chennai"},"state_map":"http://192.168.1.1/AtoZ/upload/states/jammu & kashmir.png"},{"state_id":"12","state_name":"Jharkhand","options":{"capital0":"Chennai","capital2":"Ranchi","capital1":"Chandigarh"},"state_map":"http://192.168.1.1/AtoZ/upload/states/jharkand.png"},{"state_id":"13","state_name":"Karnataka","options":{"capital0":"Chennai","capital2":"Bangalore","capital1":"Chandigarh"},"state_map":"http://192.168.1.1/AtoZ/upload/states/karnataka.png"},{"state_id":"14","state_name":"Kerala","options":{"capital0":"Srinagar ","capital2":"Thiruvananthapuram","capital1":"Bhubaneswar"},"state_map":"http://192.168.1.1/AtoZ/upload/states/kerala.png"},{"state_id":"15","state_name":"Madhya Pradesh","options":{"capital0":"Naya Raipur","capital2":"Bhopal","capital1":"Bhubaneswar"},"state_map":"http://192.168.1.1/AtoZ/upload/states/madhya pradesh.png"},{"state_id":"16","state_name":"Maharashtra","options":{"capital0":"Kolkata","capital2":"Mumbai","capital1":"Jaipur"},"state_map":"http://192.168.1.1/AtoZ/upload/states/maharastra.png"},{"state_id":"17","state_name":"Manipur","options":{"capital0":"Bhopal","capital2":"Imphal","capital1":"Chennai"},"state_map":"http://192.168.1.1/AtoZ/upload/states/manipur.png"},{"state_id":"18","state_name":"Meghalaya","options":{"capital0":"Chennai","capital2":"Shillong","capital1":"Chandigarh"},"state_map":"http://192.168.1.1/AtoZ/upload/states/meghalaya.png"},{"state_id":"19","state_name":"Mizoram","options":{"capital0":"Bhopal","capital2":"Aizawl","capital1":"Chennai"},"state_map":"http://192.168.1.1/AtoZ/upload/states/mizoram.png"},{"state_id":"20","state_name":"Nagaland","options":{"capital0":"Thiruvananthapuram","capital2":"Kohima","capital1":"Lucknow"},"state_map":"http://192.168.1.1/AtoZ/upload/states/nagaland.png"},{"state_id":"21","state_name":"Odisha","options":{"capital0":"Srinagar ","capital2":"Bhubaneswar","capital1":"Kohima"},"state_map":"http://192.168.1.1/AtoZ/upload/states/odisha.png"},{"state_id":"22","state_name":"Punjab","options":{"capital0":"Srinagar ","capital2":"Chandigarh","capital1":"Kohima"},"state_map":"http://192.168.1.1/AtoZ/upload/states/punjab.png"},{"state_id":"23","state_name":"Rajasthan","options":{"capital0":"Chennai","capital2":"Jaipur","capital1":"Bhubaneswar"},"state_map":"http://192.168.1.1/AtoZ/upload/states/rajasthan.png"},{"state_id":"24","state_name":"Sikkim","options":{"capital0":"Srinagar ","capital2":"Gangtok","capital1":"Kohima"},"state_map":"http://192.168.1.1/AtoZ/upload/states/sikkim.png"},{"state_id":"25","state_name":"Tamil Nadu","options":{"capital0":"Shimla","capital2":"Chennai","capital1":"Srinagar "},"state_map":"http://192.168.1.1/AtoZ/upload/states/tamil nadu.png"},{"state_id":"26","state_name":"Telangana","options":{"capital0":"Gangtok","capital2":"Hyderabad","capital1":"Bhubaneswar"},"state_map":"http://192.168.1.1/AtoZ/upload/states/telangana.png"},{"state_id":"27","state_name":"Tripura","options":{"capital0":"Bhopal","capital2":"Agartala","capital1":"Gangtok"},"state_map":"http://192.168.1.1/AtoZ/upload/states/tripura.png"},{"state_id":"28","state_name":"Uttar Pradesh","options":{"capital0":"Mumbai","capital2":"Lucknow","capital1":"Jaipur"},"state_map":"http://192.168.1.1/AtoZ/upload/states/uttarakhand.png"},{"state_id":"29","state_name":"Uttarakhand","options":{"capital0":"Kolkata","capital2":"Dehradun","capital1":"Chandigarh"},"state_map":"http://192.168.1.1/AtoZ/upload/states/uttar pradesh.png"},{"state_id":"30","state_name":"West Bengal","options":{"capital0":"Dehradun","capital2":"Kolkata","capital1":"Chandigarh"},"state_map":"http://192.168.1.1/AtoZ/upload/states/west bengal.png"}]}
"""
// convert String to Data, if you've data directly use it
let data = str.data(using: .utf8)
// convert Data to 'AnyObject'
let anyObj = try! JSONSerialization.jsonObject(with: data!, options: .init(rawValue: 0)) as! [String: AnyObject]
let stateList = anyObj["states_list"] as! [[String: AnyObject]]
var arrayOfCities = [String]()
for state in stateList {
let options = state["options"] as! [String: String]
arrayOfCities.append(options["capital0"]!)
}
print(arrayOfCities)
注意我强行包裹对象,请注意