• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Checking if a string is valid JSON
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Checking if a string is valid JSON


  • Subject: Checking if a string is valid JSON
  • From: "Eric E. Dolecki" <email@hidden>
  • Date: Mon, 29 Jan 2018 20:17:09 +0000

I am generating a String of JSON. Before I use it, I want to check to make
sure that it's valid. My code is below. Does this look alright?

Thanks,
Eric



        let jsonString = composedString

        let jsonData = jsonString.data(using: String.Encoding.utf8)

        do {

            if (try JSONSerialization.jsonObject(with: jsonData!, options:
[]) as? NSDictionary) != nil {

                print("JSON is a dictionary. Valid.")

            } else {

                print("Not valid JSON data.")

            }

        } catch let error as NSError {

            print("Not valid JSON. \(error.localizedDescription)")

        }
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Checking if a string is valid JSON
      • From: Jens Alfke <email@hidden>
    • Re: Checking if a string is valid JSON
      • From: Saagar Jha <email@hidden>
  • Prev by Date: Re: Crash in Touchbar
  • Next by Date: Re: Checking if a string is valid JSON
  • Previous by thread: Re: Crash in Touchbar
  • Next by thread: Re: Checking if a string is valid JSON
  • Index(es):
    • Date
    • Thread