Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
937 views
in Technique[技术] by (71.8m points)

swift - error processing json data: The data couldn’t be read because it isn’t in the correct format

This error has been annoying all morning, I'm trying to retrieve JSON in the format:

song = {
'artist':'Tom Waits',
'song':'New Coat Of Paint',
'lyrics':'Let's put a new coat of paint on this lonesome old town
Set 'em up, we'll be knockin' em [...]',
'url':'http://lyrics.wikia.com/Tom_Waits:New_Coat_Of_Paint'
}

from this URL: JSON URL

This is the function I'm using to parse the data:

  func parseJSONFromData(_ jsonData: Data?) -> [String : AnyObject]?
  {
    if let data = jsonData {
      do {
        let jsonDictionary = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as? [String : AnyObject]//Parses data into a dictionary

        return jsonDictionary

      } catch let error as NSError {
        print("error processing json data: (error.localizedDescription)")
      }
    }

    return nil
  }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you go to the site http://json.parser.online.fr it will let you paste in data and check to see if it's valid JSON or not. Yours is not.

As others have said, strings need to be enclosed in double quotes, not single quotes, and the "=" is not valid.

It would be quite easy to write code that would replace all occurrences of ' with ". The song = bit is less clear. The correct format for JSON would be:

{
  "song": {
    "artist":"Tom Waits",
    "song":"New Coat Of Paint",
    "lyrics":"Let"s put a new coat of paint on this lonesome old town
Set "em up, we"ll be knockin" em [...]",
    "url":"http://lyrics.wikia.com/Tom_Waits:New_Coat_Of_Paint"
  }
}

Or you could get rid of the outer dictionary entirely:

  {
    "artist":"Tom Waits",
    "song":"New Coat Of Paint",
    "lyrics":"Let"s put a new coat of paint on this lonesome old town
Set "em up, we"ll be knockin" em [...]",
    "url":"http://lyrics.wikia.com/Tom_Waits:New_Coat_Of_Paint"
  }

You need to look at your data and figure out what's wrong with it in the general case that makes it not legal JSON.

EDIT:

After further digging, it seems that the URLs you're using are returning JavaScript, not JSON. Try an URL like this instead:

http://lyrics.wikia.com/wikia.php?controller=LyricsApi&method=getSong&artist=Tom%20Waits&song=new%20coat%20of%20paint

That should give you the lyrics of the Tom Waits song New Coat of Paint in well-formed JSON.

This Github page gives info on the search parameters you can use to query that site and get lyrics:

https://github.com/Wikia/app/blob/dev/extensions/wikia/LyricsApi/LyricsApiController.class.php#L10-L15


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...