我正在开发的项目通过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"]
}
}