开源中国

我们不支持 IE 10 及以下版本浏览器

It appears you’re using an unsupported browser

为了获得更好的浏览体验,我们强烈建议您使用较新版本的 Chrome、 Firefox、 Safari 等,或者升级到最新版本的IE浏览器。 如果您使用的是 IE 11 或以上版本,请关闭“兼容性视图”。
randomjson首页、文档和下载 - 根据条件 JSON 生成随机 JSON - 开源中国社区
Float_left Icon_close
全部项目分类
我要评价
MIT
JavaScript 查看源码»
跨平台
周胜
分享
收藏
4 人收藏
收录时间:2017-04-10
randomjson 详细介绍

randomjson,前端 mock 利器。可根据条件 json 生成随机 json。

用法

    // 在amd,cmd环境中,引用包,在浏览器中无需引用
    
    var randomjson = require('randomjson');
    
    // 模型
    var modelJson = {
        "code": "00",
        "numberCode": "<@[10000,20000]>",
        "msg": "<@chinese{12,50}>",
        "msg2": "<@string{2,3}>",
        "logo": "<@image{100,100}>",
        "result": {
            "pList<@{1,3}>":[ 
                {
                    "indexToString": "<@index><@>",
                    "index": "<@index>",
                    "id": "<@[1-5]>", 
                    "price": "<@float>",
                    "name": "公司名称<@index>",
                    "person": "李文武<@index>",
                    "address": "北京市海淀区西三旗",
                    "mobile": "1<@number{10}>",
                    "tel": "<@number{4}>-<@number{8}>",
                    "list": [
                        {
                            "auditKey": 1,
                            "auditValue": "<@[0,1,2]>"
                        }, 
                        {
                            "auditKey": 2,
                            "auditValue": "<@[0,1,2]>"
                        }
                    ]
                }
            ]
        }
    }
    // 根据模型生成json
    var myJson = randomjson(modelJson);
    
    根据上边模型生成的json可能是这样:
    
    {
        "code": "00",
        "numberCode": 10000,
        "msg": "加义为边平压你治提用根治问求只或程干立农资特",
        "msg2": "dv",
        "logo": "https://dummyimage.com/100x100",
        "result": {
            "pList": [
                {
                    "indexToString": "1",
                    "index": 1,
                    "id": 2,
                    "price": 93.78,
                    "name": "公司名称1",
                    "person": "李文武1",
                    "address": "北京市海淀区西三旗",
                    "mobile": "17929438781",
                    "tel": "1148-56055642",
                    "list": [
                        {
                            "auditKey": 1,
                            "auditValue": 0
                        },
                        {
                            "auditKey": 2,
                            "auditValue": 1
                        }
                    ]
                },
                {
                    "indexToString": "2",
                    "index": 2,
                    "id": 5,
                    "price": 29.49,
                    "name": "公司名称2",
                    "person": "李文武2",
                    "address": "北京市海淀区西三旗",
                    "mobile": "17826945504",
                    "tel": "7298-46226026",
                    "list": [
                        {
                            "auditKey": 1,
                            "auditValue": 0
                        },
                        {
                            "auditKey": 2,
                            "auditValue": 0
                        }
                    ]
                }
            ]
        }
    }

大家对 randomjson 的评论 (全部 0 条评论)
{{repayCom.userName}}
顶部