我正在开发的一个项目通过 JSON 文件读取不同动物物种的数据。其中大约有 150 多个,除了其中的 19 个之外,大多数都可以很好地阅读。这些仅返回 [key: value]。我在其中找不到任何格式错误,并且开始认为其中有某种不可见的字符。虽然,我可能是错的。解析它虽然只有不同的读者似乎记录了不同的错误,但我无法很好地解释这些错误以从中获得任何重要的线索。这是注册为 [key: value] 的 JSON 文件之一。下面是我用来阅读它们的功能。
{
"name":"Slender Waterweed",
"scientific name":"Elodea nuttallii",
"pictures":
[
"slender_waterweed1.jpg",
"slender_waterweed2.jpg",
"slender_waterweed3.jpg"
],
"information":
{
"Family":
[
"Hydrocharitaceae"
],
"Identifying Characteristics":
[
"Submersed aquatic plant",
"Stems: long, slender, branched",
"Leaves: lance-shaped leaves that are longer, more flimsy and slender (1.3mm wide) attached directly to stem (no petiole) in whorls of 3, finely serrated ",
"Flowers: early to mid summer, small white with 3 petals, produced at tips of long slender stalks that rise above water surface",
"Fruits/Seeds: capsule, 6mm long",
"Other:"
],
"Biology":
[ "Perennial",
"Primary reproduction: fragmentation",
"Reproduction by seed (rare)"
],
"Habitat":
[ "Submersed plant community",
"Prefer fine, nutrient-rich sediment",
"Freshwater ponds, lakes, slow moving streams, tidal tributaries"
],
"Look Alikes":
[
"Brazilian Waterweed",
"Hydrilla",
"Common Waterweed",
"Mare's Tail"
],
"Commonly Seen":
[
"Midwater - Shallow"
],
"Range":
[ "Native Range: Maine, New England, and much of the United States"
]
},
"tags":
[
"flora",
"vascular aquatic plants",
"plants with blade-shaped leaves on stems",
"leaves arranged in whorls",
"shallow"
]
}
这是我用来解析 JSON 文件的代码。
func jsonSpeciesResponse(location: String) -> [String : AnyObject] {
let path = NSBundle.mainBundle().pathForResource("species JSON files (updated/" + location, ofType: "json")
let data = NSData(contentsOfFile: path!, options: NSDataReadingOptions.DataReadingUncached, error: nil)
let json: AnyObject! = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers, error: nil)
if(!(json == nil)){
return json as! [String : AnyObject]
}else{
return ["key":"value"]
}
}