Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why can't we parse a rawString back to json object? #101

Closed
artworkad opened this issue Nov 25, 2014 · 13 comments
Closed

Why can't we parse a rawString back to json object? #101

artworkad opened this issue Nov 25, 2014 · 13 comments

Comments

@artworkad
Copy link

var obj = JSON("...")
var str = obj.rawString()
var anotherObj = JSON(str)

this does not work. Why?

@LukeTangPL
Copy link
Member

try

var anotherObj = JSON(str!)

@frankykl
Copy link

frankykl commented Dec 6, 2014

There is a problem after parsing rawString back to json object though:

let json = JSON(["user":"name"])
println(json["user"]) --> name
let jsonStr = json.rawString()!
let json2 = JSON(jsonStr)
println(json2["user"]) --> null

What may I have done wrong? Thanks!!

@darktiny
Copy link

darktiny commented Dec 9, 2014

Same issue here.

let json = JSON(["user":"name"])
println(json["user"])
let data = json.rawString()!.dataUsingEncoding(NSUTF8StringEncoding)!
let json2 = JSON(data: data)
println(json2["user"])

It works

@LukeTangPL
Copy link
Member

Try

public func rawData(options opt: NSJSONWritingOptions = NSJSONWritingOptions(0), error: NSErrorPointer = nil) -> NSData?

@yungfu
Copy link

yungfu commented Sep 14, 2015

I got the same issue with version 1.3.1
var obj = JSON("...")
var str = obj.rawString()
var anotherObj = JSON(str)
I cannot fetch any property of "anotherObj". Yes, rawData() can work here, but I really need store the string value, why it cannot unserialize the rawString

@peterjuras
Copy link

I had the same issue. I got it working by using JSON.parse(myString) instead

@korgx9
Copy link

korgx9 commented Jan 15, 2016

How to remove white spaces and newlines with rawString()?

@zizhengwu
Copy link

I came across the same issue and wasted a lot of time here. The solution provided by @peterjuras finally solve my problem. This is definitely an awful bug.

@tommc1985
Copy link

I've had the same issue, have been doubting myself for days. Thank you @peterjuras, you're solution worked.

@pawangiri
Copy link

I feel the above solution should be part of the official document. @peterjuras Thanks for sharing.

@r-a-o
Copy link

r-a-o commented May 2, 2017

JSON.parse is deprecated, use JSON.init(parseJSON: "your json string")

@leochoo
Copy link

leochoo commented Jan 30, 2019

JSON.parse is deprecated, use JSON.init(parseJSON: "your json string")

Where did you find this info??

@r-a-o
Copy link

r-a-o commented Feb 4, 2019

@leochoo A quick search through the SwiftyJSON.swift source file looking for init() methods.

public init(parseJSON jsonString: String) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests