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

Updating a json #60

Closed
yelled3 opened this issue Oct 2, 2014 · 9 comments
Closed

Updating a json #60

yelled3 opened this issue Oct 2, 2014 · 9 comments

Comments

@yelled3
Copy link

yelled3 commented Oct 2, 2014

Hi, is there a way to update a JSON object? e.g

let json = JSON(data: dataFromNetworking)
json[0]["user"]["name"] = "new user name"
@k06a
Copy link
Contributor

k06a commented Oct 2, 2014

You need to fully rewrap your edited object in JSON enum or propose another genius solution for editing SwiftyJSON :)

@k06a
Copy link
Contributor

k06a commented Oct 2, 2014

Try this fork: k06a/SwiftyJSON/tree/editing I think this functionality will be implemented in SwiftyJSON soon, method names and form of calls may be changed.

@LukeTangPL
Copy link
Member

Following NSJSONSerialization.JSONObjectWithData there are mutable data in JSON, but the first thing is set the data safety. If I do not know the type of the data, I can't set the data to unknown type. If I do know the type, I can set the data like this:

var newJson: JSON?
if let dictionary = json[999]["user"].dictionary {
    //It's safe
    var newObject = json.array
    (newObject[999] as Dictionary<String, AnyObject>)!["user"] = "new user name"
    newJson = JSON(object: newObject)
} else {
    //do something where dictionary is not the Dictionary<String, Anyobject>
}

@yelled3
Copy link
Author

yelled3 commented Oct 2, 2014

Hi @k06a, thanks for the reply :-)

I assume you're referencing:
https://github.com/k06a/SwiftyJSON/blob/editing/Source/SwiftyJSON.swift#L229-L263

I could find any other usage (tests or documentation)...

@k06a
Copy link
Contributor

k06a commented Oct 2, 2014

@yelled3 you can see example of usage in my PR: #61

@k06a
Copy link
Contributor

k06a commented Oct 3, 2014

@yelled3 just added new syntax:

let json2 = json.updated((0,"user","name"), newValue: JSON(object: "Anton Bukov"))

It is mush shorter and readable :)

@LukeTangPL
Copy link
Member

@k06a @yelled3
I will release the 2.0.0 version of SwiftJSON in a few days, like this:

var json = JSON(object: object)
value[0]["id"] = "12345667"
println(value[0]["id"]) // print "1234567"
value[0]["id"] = "789012"
println(value[0]["id"]) // print "789012"
var json1:JSON = 12345677
var json2:JSON = "hello"
var json3:JSON = [1,2,3]
var json4:JSON = ["name":"tangplin",list:[1,2,3,4]]
//Update
json1= 456
json2= "www"
json3[0] = 4
json4["name"] = "Jack"

It's very cool!

@k06a
Copy link
Contributor

k06a commented Oct 4, 2014

Looks most native way to update a nested JSON. Are you using mutable collections inside JSON enum?

@LukeTangPL
Copy link
Member

Done in 59996b2 748ef28 eb7cc54 at develop branch.
It's need more test before merge into the master branch.

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

3 participants