gpt4 book ai didi

ios - Swift JSON 项目数组

转载 作者:行者123 更新时间:2023-11-30 14:12:20 24 4
gpt4 key购买 nike

大家好,我有一个问题。我在 swift 中使用 JSON 来获取有关用户的信息,因此响应是:

{
"user_details": {
"name":"samsun",
"email_id":"charchit2@ymail.com",
"mobile":"9503322593",
"phone":"NA",
"phone_os":"NA",
"devicetoken":"",
"city":"Mumbai",
"state":"Maharashtra",
"DOB":"0000-00-00",
"gender":"Female",
"address":"Shanti apts, A105",
"pinCode":"400068",
"isRegistered":"verified",
"imei":""
},
"vehicles": {
"1": {
"vechicle_no":"MH03AW0001",
"model_no":"Alto 800",
"variant":"LXI",
"color":"White",
"date_of_purchase":"10/10/2014",
"insurance_company_name":"Bajaj",
"insurance_expiry_date":"10/10/2015",
"created_at":"2015-06-02 23:31:30",
"fuelType":"CNG",
"vinNumber":"HSJHFJ10"
},
"2": {
"vechicle_no":"MH48S8403",
"model_no":"Swift",
"variant":"VXI",
"color":"AZURA GREY",
"date_of_purchase":"17/10/2007",
"insurance_company_name":"NEW INDIA",
"insurance_expiry_date":"17/10/2007",
"created_at":"2015-06-27 22:22:57",
"fuelType":"Petrol",
"vinNumber":"ABCD0316"
}
},
"status":"1",
"verified":"true"
}

好的,现在我需要保存车号(vehicle_no),但是因为有不同的用户,我需要保存第一个车号、第二个车号、第三个车号等。例如,这个用户有两个车号,我可以看到它,因为“2”后面有第二个车号。那么我怎样才能获得这两个车牌号呢?如果用户有的话,我还需要能够读取第三、第四个车辆号码。抱歉,如果我没有解释清楚,但提前谢谢您。

这是我现在的 json 代码。

 func MyVehicleInfo() {

let Mobile = save.stringForKey("USERNAME")!

let post:NSString = "mobile=9503322593"

NSLog("PostData: %@",post);

let url:NSURL = NSURL(string: "http://saimobileapp.com/services/sai_authenticate.php")!

let postData:NSData = post.dataUsingEncoding(NSASCIIStringEncoding)!

let postLength:NSString = String( postData.length )

let request:NSMutableURLRequest = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.HTTPBody = postData
request.setValue(postLength as String, forHTTPHeaderField: "Content-Length")
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")


var reponseError: NSError?
var response: NSURLResponse?

var urlData: NSData? = NSURLConnection.sendSynchronousRequest(request, returningResponse:&response, error:&reponseError)
println(urlData)
if ( urlData != nil ) {
let res = response as! NSHTTPURLResponse!;

NSLog("Response code: %ld", res.statusCode);
if (res.statusCode >= 200 && res.statusCode < 300)
{
let responseData:NSString = NSString(data:urlData!, encoding:NSUTF8StringEncoding)!

NSLog("Response ==> %@", responseData);

var error: NSError?

let jsonData:NSDictionary = NSJSONSerialization.JSONObjectWithData(urlData!, options:NSJSONReadingOptions.MutableContainers , error: &error) as! NSDictionary




let vehiclenodb = ((jsonData ["vehicles"] as! NSDictionary)["1"]! as! NSDictionary)["vechicle_no"]! as! String
let modelnodb = ((jsonData ["vehicles"] as! NSDictionary)["1"]! as! NSDictionary)["model_no"]! as! String
let variantdb = ((jsonData ["vehicles"] as! NSDictionary)["1"]! as! NSDictionary)["variant"]! as! String
let colordb = ((jsonData ["vehicles"] as! NSDictionary)["1"]! as! NSDictionary)["color"]! as! String
let datepurchasedb = ((jsonData ["vehicles"] as! NSDictionary)["1"]! as! NSDictionary)["date_of_purchase"]! as! String
let insurancecompanynamedb = ((jsonData ["vehicles"] as! NSDictionary)["1"]! as! NSDictionary)["insurance_company_name"]! as! String
let insuranceexpirydatedb = ((jsonData ["vehicles"] as! NSDictionary)["1"]! as! NSDictionary)["insurance_expiry_date"]! as! String
let fueltypedb = ((jsonData ["vehicles"] as! NSDictionary)["1"]! as! NSDictionary)["fuelType"]! as! String
//var emaildb = ((jsonData as NSDictionary)["user_details"] as! NSDictionary) ["email_id"] as! String



var vehicleno:Void = save.setObject(vehiclenodb, forKey: "VehicleNumberSave")
var modelno:Void = save.setObject(modelnodb, forKey: "ModelNumberSave")
var variant:Void = save.setObject(variantdb, forKey: "VariantSave")
var color:Void = save.setObject(colordb, forKey: "ColorSave")
var datepurchase:Void = save.setObject(datepurchasedb, forKey: "DateofPurchaseSave")
var insurancecompanyname:Void = save.setObject(insurancecompanynamedb, forKey: "InsuranceCompanySave")
var insuranceexpirydate:Void = save.setObject(insuranceexpirydatedb, forKey: "InsuranceExpirySave")
var fueltype:Void = save.setObject(fueltypedb, forKey: "FuelTypeSave")

save.synchronize()





}
}



}

最佳答案

这是你必须处理的一些丑陋的 JSON。您可以像这样提取车辆。

if let vehicles = jsonData["vehicles"] as? NSDictionary {
let vehiclesKeys = vehicles.allKeys
for key in vehiclesKeys {
println(vehicles[key])
}
}

关于ios - Swift JSON 项目数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31635597/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com