Swiftyjson: How to print JSON in short way. Without \n and spaces

Created on 18 Dec 2017  路  6Comments  路  Source: SwiftyJSON/SwiftyJSON

A json like this:

{
  "data": {
    "cat" : "Tom",
    "dog" : "Teddy"
  }
}

Tried:

json.rawString()
json.rawString([:])

Expect:

{"dat":{"cat":"Tom","dog":"Teddy"}}

Happened:

{n "data" : {n "cat" : "Tom",n "dog" : "Teddy"n }n}

//formated

Environment:

  • SwiftyJSON: 4
  • Xcode Version: 9
  • Swift Version: 4

Related issue

837

Most helpful comment

Try this
json.rawString(.utf8, options: .init(rawValue: 0))

All 6 comments

Now I did like this. It works well so far.
import SwiftyJSON extension JSON { func toString() -> String? { return self.rawString()?.replacingOccurrences(of: "\\s", with: "", options: String.CompareOptions.regularExpression, range: nil) } }
But not tested enough for more situations.

And, Asking for a better function name to replace "toString:" :-<

Seems like #837 doesn't work in Swift 4. Any idea to create a PR for this?

Try this
json.rawString(.utf8, options: .init(rawValue: 0))

Same issue

Try this
json.rawString(.utf8, options: .init(rawValue: 0))

This works for me in Swift 4, SwiftyJSON 4

Linked to #966

Was this page helpful?
0 / 5 - 0 ratings

Related issues

posixrails picture posixrails  路  6Comments

amit-bhavsar picture amit-bhavsar  路  5Comments

donadley picture donadley  路  4Comments

bloqlist picture bloqlist  路  7Comments

AndrewC-B picture AndrewC-B  路  5Comments