Reputation: 315
I'm making a diagram in d3.js which uses hierarchical data. It is going to be interactive, so you can add data values, children etc. My question is fairly simple: is there a way I can dynamically use JSON in the browser, that is edit the hierarchy and add/remove data from it. If not, are there any alternatives I could use?
Thanks in advance!
Upvotes: 0
Views: 911
Reputation: 1638
Convert your JSON to an object using JSON.parse (you might need the JSON2 library to polyfill), make your modifications as needed, and when you need the JSON back (ex. to send it back to the server), reconvert it to string with JSON.stringify
Upvotes: 1
Reputation: 1293
Not sure if that helps but JSON is nothing more than a normal JS object, which you define with
var foo = {
name: "foo"
type: "bar"
children: [{}, {}]
}
If you have a string that represents a JSON, you can just parse it:
var json = '{"name":"foo","type":"bar",children: [{}, {}]}',
obj = JSON.parse(json);
you can add/remove data via normal javascript. Here you can find more about this: http://www.json.org/js.html
Upvotes: 1