使用随机数据生成 JSON

Jon*_*Jon 2 javascript jquery json

我正在使用opportunity.js来帮助我生成一些随机数据。它运行得非常好,我想使用这些数据来输出随机 JSON 对象。

这是我想要的结构:

{
    "users": [
        {
            "Name": "John Smith",
            "Fname": "John",
            "Lname": "Smith",
            "Email": "john.smith@acmemedia.com", 
        }, 

        ...etc
}
Run Code Online (Sandbox Code Playgroud)

我有opportunity.js在 jsFiddle 中工作,但我不确定如何使用chance.js函数输出 JSON 数据。

$(function() {    
    $("div[data]").each(function() {
        var data = $(this).attr("data");
        var chance = new Chance(Math.Random);
        $(this).append(chance[data]());
    });
});
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/5Lcfz838/4/

opportunity.js 文档可在此处获取: http: //chancejs.com/

lor*_*ond 7

这是一个包含 10 个随机对象的数组:

var arr = new Array(10).fill().map(function() {
    return {
        first: chance["first"](),
        last: chance["last"](),
        email: chance["email"](),
        city: chance["city"](),
        state: chance["state"](),
    };
});

var json = JSON.stringify({ users: arr }, null, 2); // as you asked for json
console.log(json);
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/chance/0.5.6/chance.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Run Code Online (Sandbox Code Playgroud)