最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to stringify circular JSON object and save it to file? - Stack Overflow

programmeradmin1浏览0评论

I'm working on a radial tree diagram in javascript using d3.js and jquery. I use json files to load data into a javascript var. After working on a diagram and making some changes, e.g. changing parent of some nodes, I want to stringify data and save as a json file. When I stringify treeData variable, which includes all json data, and wanted to display it, I receive only the root node stringified. This is my code:

var json = JSON.stringify(treeData.children, function(key,value){
    if(key == "children"){
        return undefined;
    }
    return value;
});
alert("Json: " + json);

Also I don't know why the key is set to "children". My JSON file structure:

{
 "name": "root",
 "children": [
  {
   "name": "child1",
   "children": [ ... ]
  }
 ]
}

I'm working on a radial tree diagram in javascript using d3.js and jquery. I use json files to load data into a javascript var. After working on a diagram and making some changes, e.g. changing parent of some nodes, I want to stringify data and save as a json file. When I stringify treeData variable, which includes all json data, and wanted to display it, I receive only the root node stringified. This is my code:

var json = JSON.stringify(treeData.children, function(key,value){
    if(key == "children"){
        return undefined;
    }
    return value;
});
alert("Json: " + json);

Also I don't know why the key is set to "children". My JSON file structure:

{
 "name": "root",
 "children": [
  {
   "name": "child1",
   "children": [ ... ]
  }
 ]
}
Share Improve this question asked Jan 11, 2015 at 21:52 NojasNojas 631 gold badge2 silver badges7 bronze badges 2
  • 2 You are specifically excluding the children properties when serialising the object, how do you expect the result being anything but just the root node without the children property? – Guffa Commented Jan 11, 2015 at 21:57
  • 1 You're right. The structure of the object in js var is not the same as the json file I loaded. I just change "children" to "parent" and it's working properly. It came out to me a moment earlier I read your answer but thanks! – Nojas Commented Jan 11, 2015 at 22:41
Add a ment  | 

4 Answers 4

Reset to default 3

You have a children key in your object literal so it is passed to the callBack and then for some reason you are returning undefined, which will remove that key from the result.

Circular JSON Objects can be stringified and saved with the following approach:

import stringify from 'json-stringify-safe';
import fs from 'fs';

const content = stringify(myCircularJSONObject);

fs.writeFile('./data.json', content, ()=>{console.log('CREATED')});

The problem is that using tree data will return with a loop and circular data error. Switching key from "children" to "parent" give a json without references to the parents and work properly.

If I understand correctly you are trying to achieve stringifying of an object structured as:

var obj = {};
obj.circle = obj;
var str = JSON.stringify(obj);
// → Error

This object will return an error with JSON.stringify although is otherwise valid. If you don't mind the readability of the stringified object, I suggest using CircularJSON:

Install when using Node.js:

npm install --save circular-json

Install for web:

<script src="build/circular-json.js"></script>

And usage:

var object = {};
object.arr = [
    object, object
];
object.arr.push(object.arr);
object.obj = object;

var serialized = CircularJSON.stringify(object);
// → '{"arr":["~","~","~arr"],"obj":"~"}'

var unserialized = CircularJSON.parse(serialized);
// → { arr: [ [Circular], [Circular] ], obj: [Circular] }
发布评论

评论列表(0)

  1. 暂无评论