[英]Xcode, PHP; Having trouble with retrieving data from PHP/MySQL with SwiftyJSON-master
I'm developing an iOS app with Xcode and Swift. 我正在使用Xcode和Swift开发一个iOS应用程序。
I need to load some data from my MySQL database in my app. 我需要在我的应用程序中加载MySQL数据库中的一些数据。 I think it's reasonable to do this with JSON (or is there something against it?).
我认为用JSON做这个是合理的(或者有什么东西反对它?)。
To make it easier I want to use SwiftyJSON-master . 为了更容易,我想使用SwiftyJSON-master 。
I found this working code on SO: 我在SO上找到了这个工作代码:
class ViewController: UIViewController {
var dict = NSDictionary()
@IBOutlet weak var authorLbl: UILabel!
@IBOutlet weak var quoteLbl: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let urlString = "http://api.theysaidso.com/qod.json"
if let url = NSURL(string: urlString) {
if let data = try? NSData(contentsOfURL: url, options: []) {
let json = JSON(data: data)
print(json)
let auther = json["contents"]["quotes"][0]["author"].stringValue
let quote = json["contents"]["quotes"][0]["quote"].stringValue
authorLbl.text = auther
quoteLbl.text = quote
}
}
}
}
But when trying it with my own PHP script it doesn't work. 但是当用我自己的PHP脚本尝试它时它不起作用。
I think it's because of the different outputs. 我认为这是因为产出不同。 The (working) PHP script from the SO post I found is like this:
我找到的SO帖子中的(工作)PHP脚本是这样的:
{"success":{"total":1},"contents":{"quotes":[{"quote":"I put instant coffee in a microwave oven and almost went back in time.","length":"70","author":"Steven Wright","tags":["coffee","funny","humor","thetimes","time"],"category":"funny","id":"LvFg2QsKr_FoCH_lwryQ5geF"}]}}
Using my own PHP scrip output is like this: [{"a":"John Eve","b":"Cupertino","c":"Green","d":"Apple"},{"a":"Sarah Jones","b":"Berlin","c":"Red","d":"Pear"}]
使用我自己的PHP脚本输出是这样的:
[{"a":"John Eve","b":"Cupertino","c":"Green","d":"Apple"},{"a":"Sarah Jones","b":"Berlin","c":"Red","d":"Pear"}]
The syntax seem to bee completely different. 语法似乎完全不同。
This is my PHP script/code: 这是我的PHP脚本/代码:
<?php
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
include "$root/config.php";
$query = "SELECT * FROM users";
$stmt = $pdo->prepare($query);
$stmt->execute();
$userData = array();
while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
$userData[] = $row;
}
echo json_encode($userData);
?>
What am I doing wrong? 我究竟做错了什么? How can I influence the syntax?
我该如何影响语法? Or is it another problem?
还是另一个问题?
PS: Is it reasonable to use this library or is something against it? PS:使用这个库是否合理或反对它?
Your php file returns a correct json format. 您的php文件返回正确的json格式。 You can access to the data simply like this:
您可以像这样访问数据:
print(json[0]["a"]) // John Eve
print(json[0]["b"]) // Cupertino
// ...
print(json[1]["a"]) // Sarah Jones
print(json[1]["c"]) // Red
In pursuit of understanding why is there a difference between the previous two JSON syntaxes, you need to learn about JSON Syntax . 为了理解为什么前两个JSON语法之间存在差异,您需要了解JSON语法 。
I will try to explain that to you very briefly. 我将非常简短地向您解释一下。 I know that I'm bad at writing in English, but I'll do my best.
我知道我用英文写作不好,但我会尽我所能。
The curly braces in json syntax represent a json object like: json语法中的花括号表示json对象,如:
{"a":"John Eve","b":"Cupertino","c":"Green","d":"Apple"}
{"a":"Sarah Jones","b":"Berlin","c":"Red","d":"Pear"}
The square brackets in json syntax represent an array of json objects like: json语法中的方括号表示json对象的数组,如:
[
{"a":"John Eve","b":"Cupertino","c":"Green","d":"Apple"},
{"a":"Sarah Jones","b":"Berlin","c":"Red","d":"Pear"}
]
So, in the theysaidso's case, the JSON syntax represents a single JSON object that is: 因此,在theysaid的情况下,JSON语法表示单个JSON对象:
The JSON object above has two JSON objects inside of it, the first object is: 上面的JSON对象里面有两个JSON对象,第一个对象是:
"success":{"total":1}
the second one represents the contents: 第二个代表内容:
Inside of the content of the JSON object, there is a JSON object called quotes which hold an array of json objects :) 在JSON对象的内容中,有一个名为quotes的JSON对象,它包含一个json对象数组:)
Therefore, when you want to access an object of that nested json object you do this: 因此,当您要访问该嵌套json对象的对象时,请执行以下操作:
json["contents"]["quotes"][0]["author"].stringValue
In the above code you say: return a property of string type called "author" -["author"]- that is in a json object at index of 0 -["quotes"][0]- in the quotes array in the contents object -["contents"]- in my json object -json-! 在上面的代码中你说:返回一个名为“author”的字符串类型的属性 - [“author”] - 它位于索引为0的json对象中 - [“quotes”] [0] - 在引号数组中内容对象 - [“内容”] - 在我的json对象-json-中!
As for your case, you have a single json object that is an array of JSON objects: 至于你的情况,你有一个json对象,它是一个JSON对象数组:
Therefore, when you want to access an object of that nested JSON object you do this: 因此,当您要访问该嵌套JSON对象的对象时,请执行以下操作:
json[0]["a"]
In the above code you say: return a property called "a" -["a"]- that is in a json object at index of 0 -json[0]- in the json object array -json-! 在上面的代码中你说:返回一个名为“a” - [“a”]的属性 - 它位于索引为0 -json [0]的json对象中 - 在json对象数组中-json-!
I hope you can understand what I'm trying to say. 我希望你能理解我想说的话。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.