鍵使用此代碼來獲取和分析:解析JSON沒有SWIFT
func getJSON(urlToRequest: String) -> NSData
{
return NSData(contentsOfURL: NSURL(string: urlToRequest))
}
func parseJSON(inputData: NSData) -> NSDictionary
{
var error: NSError?
var boardsDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(inputData, options: NSJSONReadingOptions.MutableContainers, error: &error) as NSDictionary
return boardsDictionary
}
var parsedJSON = parseJSON(getJSON("url"))
println(parsedJSON)
結果:
{
message = "";
result = (
{
BC = B;
BCL = Bbb;
C = "2014-02-13T00:00:00";
IA = 1;
MC = L;
MCL = Lll;
MN = "B-L";
MS = "165";
},
{
BC = B;
BCL = Bbb;
C = "2014-02-13T00:00:00";
IA = 1;
MC = E;
MCL = Eee;
MN = "B-E";
MS = "654";
},
{
BC = B;
BCL = Bbb;
C = "2014-02-13T00:00:00";
IA = 1;
MC = D;
MCL = Ddd;
MN = "B-D";
MS = "864";
},
);
success = 1;
}
println(parsedJSON["result"])
結果:
{
BC = B;
BCL = Bbb;
C = "2014-02-13T00:00:00";
IA = 1;
MC = L;
MCL = Lll;
MN = "B-L";
MS = "165";
},
{
BC = B;
BCL = Bbb;
C = "2014-02-13T00:00:00";
IA = 1;
MC = E;
MCL = Eee;
MN = "B-E";
MS = "654";
},
{
BC = B;
BCL = Bbb;
C = "2014-02-13T00:00:00";
IA = 1;
MC = D;
MCL = Ddd;
MN = "B-D";
MS = "864";
},
println(parsedJSONlS["result"]!["MCL"])
給 「無」
如何從每組中提取MCL值?我可以通過索引訪問值嗎?
轉到json.org所和學習JSON語法 - 只需要5-10分鐘。 「結果」是一個數組。 –