1

A project I am developing reads through JSON files for data on different animal Species. There are roughly 150+ of them, and most are read fine except for 19 of them. These return as only, [key: value]. I cannot find any formatting error in them and a beginning to think there are some sort of invisible characters in it. Though, I may be wrong. Parsing it though different only readers seem to register different errors that I cannot interpret well enough to gain any significant leads from. Here is one of the JSON files that are registering as [key: value]. Further below is the function I am using to read them.

{
    "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"

    ]
}

Here is the code I am using to parse the JSON files.

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"]
        }
    }
dyllandry
  • 113
  • 1
  • 11
  • According to http://jsonlint.com, your example is *valid* JSON. And as far as I can see, it does not contain any "invisible" characters. What makes you think so? – And why don't you use the `error` parameter to get more information about a possible problem? – Martin R Jul 22 '15 at 20:26
  • Okay this is very odd.. When I copy and paste it from StackOverflow I do not get an error. Though when I copy and paste it from xCode I DO get an error. This is the error – dyllandry Jul 22 '15 at 20:45
  • Parse error on line 16: ...ated ", "Flowers: early to m ----------------------^ Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[' – dyllandry Jul 22 '15 at 20:45
  • Pasting over my old Json file with a copy from what I posted here in my post resolves the error. – dyllandry Jul 22 '15 at 20:47
  • Do you have any idea why this might of happened Martin? I believe I did have invisible characters of some sort in the file that got resolved when I posted in here. I'd imagine that StackOverflow has a pretty kickass invisible character 'remover' script built into it's site if it is a site that has to do primarily with troubleshooting code. – dyllandry Jul 22 '15 at 20:51
  • Run your original JSON through a hex dumper and pay special attention to the characters around "early to mid...". It's possible there is some zero-width unicode character there that gets removed in the copy/paste shuffle. – Palpatim Jul 22 '15 at 20:59

0 Answers0