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

javascript - Add data to existing json object - Stack Overflow

programmeradmin0浏览0评论

So what i'm trying to achieve is creating a json flat file to store user info where i'm stuck is that i don't know how to add new nested objects that are empty and then save the json file and reload it.

what i have in my *json* file is this.

{
    "users" : {
        "test" : {

        },
        "test1" : {

        }
    }
}

I'm trying to add as many new objects as i want to it. So for example.

{
    "users" : {
        "test" : {

        },
        "test1" : {

        },
        "test2" : {

        },
        "test3" : {

        }
    }
}

My server side Javascript

    json.users.push = username;
    fs.writeFile("./storage.json", JSON.stringify(json, null, 4) , 'utf-8');
    delete require.cache[require.resolve('./storage.json')];
    json = require("./storage.json");

With this code it does not write the file so when the require is done i end up with the same file and my json object ends up like this when i console.log it

{                                                                                                                                                    
    "users": {                                                                                                                                       
        "test": {},                                                                                                                                  
        "test1": {},                                                                                                                                 
        "push": "test2"                                                                                                                                
    }                                                                                                                                                
}

Please do not remend some external module to solve something has simple as this. Also if any one can point me to a in depth json documentation that gets straight to the point with what i'm try to do it would be appreciated

So what i'm trying to achieve is creating a json flat file to store user info where i'm stuck is that i don't know how to add new nested objects that are empty and then save the json file and reload it.

what i have in my *json* file is this.

{
    "users" : {
        "test" : {

        },
        "test1" : {

        }
    }
}

I'm trying to add as many new objects as i want to it. So for example.

{
    "users" : {
        "test" : {

        },
        "test1" : {

        },
        "test2" : {

        },
        "test3" : {

        }
    }
}

My server side Javascript

    json.users.push = username;
    fs.writeFile("./storage.json", JSON.stringify(json, null, 4) , 'utf-8');
    delete require.cache[require.resolve('./storage.json')];
    json = require("./storage.json");

With this code it does not write the file so when the require is done i end up with the same file and my json object ends up like this when i console.log it

{                                                                                                                                                    
    "users": {                                                                                                                                       
        "test": {},                                                                                                                                  
        "test1": {},                                                                                                                                 
        "push": "test2"                                                                                                                                
    }                                                                                                                                                
}

Please do not remend some external module to solve something has simple as this. Also if any one can point me to a in depth json documentation that gets straight to the point with what i'm try to do it would be appreciated

Share Improve this question asked Oct 5, 2015 at 3:07 DarkrumDarkrum 1,3831 gold badge12 silver badges28 bronze badges 2
  • 2 there is no such thing as JSON object. there's object, and there's JSON. it's the difference between a cake-recipe and a cake. there is no such thing cake-recipe-cake. – David Haim Commented Oct 5, 2015 at 3:12
  • What do you think json.users.push does? – user663031 Commented Oct 5, 2015 at 4:03
Add a ment  | 

1 Answer 1

Reset to default 3

Use [] to access a dynamic key on the object

json.users[username] = {a: 1, b: 2}

Be careful naming your variable like that tho because json the way you're using it is not JSON. JSON is a string, not an object with keys.

See the below demo for distinction

var json = '{"users":{"test1":{},"test2":{}}}';
var obj  = JSON.parse(json);
var newuser = 'test3';
obj.users[newuser] = {};
console.log(JSON.stringify(obj));
//=> {"users":{"test1":{},"test2":{},"test3":{}}}

发布评论

评论列表(0)

  1. 暂无评论