初学者,所以请忍受我糟糕的编程语言和技能。
我通过PHP脚本从我的MySQL收到这个json字符串数组到我的iOS Swift 2应用程序:
(
"{\"id\":\"0\",\"category\":\"ah\",\"createdAt\":\"0000-00-00\",\"expires\":\"ah\",\"location\":\"ah\",\"country\":\"ah\",\"freetext\":\"ah\"}",
"{\"id\":\"0\",\"category\":\"hej\",\"createdAt\":\"0000-00-00\",\"expires\":\"hej\",\"location\":\"hej\",\"country\":\"hej\",\"freetext\":\"hej\"}",
"{\"id\":\"0\",\"category\":\"News\",\"createdAt\":\"0000-00-00\",\"expires\":\"Now\",\"location\":\"Here\",\"country\":\"This\",\"freetext\":\"Ah!\"}"
)
我想将每个值(" id"," category"等)附加到我自己的数组中,这些数组将填充我的tableview。你在下面看到我的方法。
import UIKit
import Parse
class DemandTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var demandIdArray: [String] = [String]()
var demandCategoryArray: [String] = [String]()
var demandCreatedAtArray: [String] = [String]()
var demandExpiresArray: [String] = [String]()
var demandLocationArray: [String] = [String]()
var demandCountryArray: [String] = [String]()
var demandFreetextArray: [String] = [String]()
@IBOutlet weak var demandTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.demandTableView.delegate = self
self.demandTableView.dataSource = self
//retrieve new demand data
self.getDemandData()
}
func getDemandData() {
let requestURL: NSURL = NSURL(string: "http://picondemand.com/demands/demand_data.json")!
let urlRequest: NSMutableURLRequest = NSMutableURLRequest(URL: requestURL)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(urlRequest) {
(data, response, error) -> Void in
let httpResponse = response as! NSHTTPURLResponse
let statusCode = httpResponse.statusCode
if (statusCode == 200) {
print("Connection OK!")
do
{
//cast data as array
let data = NSData(contentsOfURL: NSURL(string: "http://picondemand.com/demands/demand_data.json")!)
if let jsonArray = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as? NSArray
{
print("Number of demands: \(jsonArray.count)")
print(jsonArray)
for (var i = 0; i < jsonArray.count ; i++ )
{
let demands = jsonArray.objectAtIndex(i)
print("\(demands)")
/*
// GET VALUES FOR "id", "category" etc
let id:String? = demands["id"] as? String
self.demandIdArray.append(id!)
*/
}
}
} catch {
print("\(error)")
}
self.demandTableView.reloadData()
}
}
task.resume()
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
///////create a table cell
let DemandCell: DemandTableViewCell = tableView.dequeueReusableCellWithIdentifier("DemandCell") as! DemandTableViewCell
/////// customize the cell
DemandCell.idLabel.text = ("Id: \(demandIdArray[indexPath.row])")
DemandCell.categoryLabel.text = ("Category: \(demandCategoryArray[indexPath.row])")
DemandCell.createdAtLabel.text = ("Created at: \(demandCreatedAtArray[indexPath.row])")
DemandCell.expiresLabel.text = ("Expires: \(demandExpiresArray[indexPath.row])")
DemandCell.locationLabel.text = ("Location: \(demandLocationArray[indexPath.row])")
DemandCell.countryLabel.text = ("Country: \(demandCountryArray[indexPath.row])")
DemandCell.freetextLabel.text = ("Description: \(demandFreetextArray[indexPath.row])")
/////// return the cell
return DemandCell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return demandIdArray.count
}
}
我的问题是我无法获得价值;他们没有。有什么问题?
关于这个主题有很多关于SO的问题,然而,没有人对我这么做过。我已经尝试了很多随机的方法,现在我开始感到头晕。
什么是NSCFString!?
我也尝试过swiftyJSON。
答案 0 :(得分:0)
您的解析无法正常工作,因为您的JSON格式不是您想要的。通过http://www.jsoneditoronline.org查看您的JSON,您将在右侧看到您的JSON代表一个包含3个字符串的数组,而不是3个字典的数组。
如果你调整你的JSON,就像下面一个,它代表一个数组或3个字典,你可以解析ID和其中的东西: - )
[
{
"id":"0",
"category":"ah",
"createdAt":"0000-00-00",
"expires":"ah",
"location":"ah",
"country":"ah",
"freetext":"ah"
},
{"id":"0",
"category":"hej",
"createdAt":"0000-00-00",
"expires":"hej",
"location":"hej",
"country":"hej",
"freetext":"hej"
},
{"id":"0",
"category":"News",
"createdAt":"0000-00-00",
"expires":"Now",
"location":"Here",
"country":"This",
"freetext":"Ah!"
}
]
将它放在JSONEditor中,您将看到正确的表示形式。你也不需要\&#34;在JSON。