Zyyo

Just for fun

文章

24

标签

25

评论

15778

数据统计

成立

401天

文章

24篇

评论

15778条

标签

25个

最近文章

使用javscript手写json-parse

空虚使我寂寞难耐索性开整
一个非常简单的JsonParse
花了我一下午加一晚上的心思
目前已经支持对象,数组互相嵌套,数字,字符串 布尔

还未实现

严格的错误校验,本项目是根据一个标准的Json格式来实现的,没有进行格式判断,你如果拿出来一个未经严格校验的json字符串来解析,甚至有可能得到正确的结果
不支持更多数字类型
不支持特殊字符转义
不支持顶层数组(我写完才发现有这个特性)。。。

作者zyyo
项目地址
https://github.com/ZYYO666/fuckDemo
(这个项目专门放各种demo)




function Json(string, _index = 0) {
    let index = _index
    let json = string.trim()
    let jsonparse = json.split('')
    let prevAttr = ''
    let isArray = false
    let isFirst = true
    const result = {}



    //通用的处理,调用其他处理器
    function parse() {
        skip()
        if (jsonparse[index] == '{') {
            return parseDakuohao1()
        } else if (jsonparse[index] == ':') {
            return parseMaohao()
        } else if (jsonparse[index] == '"') {
            return parseString()
        } else if (jsonparse[index] == ',') {
            return parseDouhao()
        } else if (/\d/.test(jsonparse[index]) || jsonparse[index] == '-') {
            return parseNum()
        } else if (json.slice(index, index + 4) === 'true') {
            return parseTrue()
        } else if (json.slice(index, index + 5) === 'false') {
            return parseFalse()
        } else if (json.slice(index, index + 4) === 'null') {
            return parseNull()
        } else if (jsonparse[index] == '}') {
            return parseDakuohao2()
        } else if (jsonparse[index] == '[') {
            return parseArray1()
        } else if (jsonparse[index] == ']') {
            return parseArray2()
        } else if (index == jsonparse.length) {
            throw new Error('标签未闭合')
        } else {
            console.log(result);
            throw new Error('未知字符' + jsonparse[index])
        }
    }
    function skip() {
        while (jsonparse[index] == ' ' || jsonparse[index] == '\n') {
            index++
        }
    }
    function parseMaohao() {
        index++
        return parse()
    }
    function parseArray1() {
        index++
        if (prevAttr == '') {
            throw new Error('[]不能用作属性')
        }
        result[prevAttr] = []
        isArray = true
        return parse()
    }
    function parseArray2() {
        index++
        prevAttr = ''
        isArray = false
        return parse()
    }
    function parseTrue() {
        index += 4
        if (prevAttr == '') {
            throw new Error('True不能用作属性')
        }
        if (isArray) {
            result[prevAttr].push(true)
        } else {
            result[prevAttr] = true
            prevAttr = ''
        }
        return parse()
    }
    function parseFalse() {
        index += 5
        if (prevAttr == '') {
            throw new Error('False不能用作属性')
        }
        if (isArray) {
            result[prevAttr].push(false)
        } else {
            result[prevAttr] = false
            prevAttr = ''
        }
        return parse()
    }
    function parseNull() {
        index += 4
        if (prevAttr == '') {
            throw new Error('null不能用作属性')
        }
        if (isArray) {
            result[prevAttr].push(null)
        } else {
            result[prevAttr] = null
            prevAttr = ''
        }
        return parse()
    }
    function parseNum() {

        let num = ''
        while (jsonparse[index] !== '}' && jsonparse[index] !== ',' && jsonparse[index] !== ']') {
            num += jsonparse[index]
            index++
        }
        if (typeof num !== 'number' && isNaN(num)) {
            throw new Error('数字格式错误')
        }
        if (prevAttr == '') {
            throw new Error('json格式错误')
        }
        num = Number(num)
        if (isArray) {
            result[prevAttr].push(num)
        } else {
            result[prevAttr] = num
            prevAttr = ''
        }
        return parse()

    }

    function parseDakuohao1() {
        if (isFirst == true) {
            index++
            isFirst = false

        } else {
            const res = Json(json, index)
            index = res.index

            if (prevAttr == '') {
                throw new Error('错误')
            }
            if (isArray) {
                result[prevAttr].push(res.object)
            } else {
                result[prevAttr] = res.object
                prevAttr = ''
            }
        }
        return parse()
    }
    function parseDakuohao2() {
        return {
            object: result,
            index: ++index
        }
    }
    function parseDouhao() {
        index++
        return parse()
    }
    function parseString() {
        let str = ''
        index++ //第一个双引号
        while (jsonparse[index] !== '"') {
            str += jsonparse[index]
            index++
        }
        index++ //第二个双引号

        if (prevAttr == '') {
            prevAttr = str
            result[str] = undefined;
            return parse()
        }

        if (isArray) {
            result[prevAttr].push(str)
        } else {
            result[prevAttr] = str

            prevAttr = ''
        }
        return parse()
    }
    return parse()


}

const json1 = {
    id: [1, {
        a: 1,
        b: {
            a: 1,
            b: 2
        }

    }, "zyyo", 3, 4, 5],
    name: "Example Object",
    isActive: true,
    details: {
        description: "This is a complex object for testing purposes.",
        metadata: {
            version: -2.5,
            createdBy: "admin",
            isVerified: false,
            history: {
                firstCreated: "2023-01-01",
                lastUpdated: "2023-10-01",
                updates: {
                    total: 5,
                    recent: {
                        date: "2023-09-15",
                        changes: {
                            added: 3,
                            removed: 1,
                            modified: 2
                        }
                    }
                }
            }
        }
    },
    settings: {
        preferences: {
            theme: "dark",
            notifications: {
                email: true,
                sms: false,
                push: true
            }
        },
        limits: {
            maxConnections: 10,
            timeout: 30000
        }
    },
    tags: {
        category: "test",
        priority: "high",
        attributes: {
            security: "confidential",
            scope: "internal"
        }
    }
};

const testjson = JSON.stringify(json1);



try {
    console.time("json")
    for (let i = 0; i < 10000; i++) {
       Json(testjson).object;
    }
    console.timeEnd("json")
    console.time("jsonpaser")
    for (let i = 0; i < 10000; i++) {
         JSON.parse(testjson)
    }
    console.timeEnd("jsonpaser")
} catch (error) {
    console.log(error.message)
}

使用javscript手写json-parse

发布于

April 2, 2025

分类

实用分享

版权协议

MIT

评论
正在回复评论:Janessa 的评论 取消
😀
已有 185 条评论

    Good write-up. I certainly appreciate this website.
    Continue the good work!

    Jens 2026-02-03 回复

    buy poker machine united kingdom, does canada have slot machines and best usa online casino reviews, or newest online casino usa

    Osvaldo 2026-02-03 回复

    online casino free bet no deposit uk, native united statesn casino issues and united kingdom pokies no
    deposit bonus, or best australia pokies

    Marty 2026-02-03 回复

    You really make it seem so easy with your presentation but I find
    this matter to be really something that I think I would never understand.
    It seems too complicated and extremely broad for me.
    I'm looking forward for your next post, I'll try to get the
    hang of it!

    Janessa 2026-02-03 回复

    casino united kingdom, best online casino south united states and latest casino news in usa,
    or online casinos accepting usa

    the great united statesn casino everett, casino chips value usa
    and online casinos that accept apple pay usa, or native united statesn casinos
    in montana

    web Site 2026-02-03 回复

    Wow! This blog looks just like my old one! It's on a totally different subject but it has pretty much the
    same page layout and design. Wonderful choice of colors!

    Casimira 2026-02-03 回复

    casinos in toronto ontario australia, top online pokies and casinos canadian update and
    usa slot machine categories, or united statesn heritage poker table

感谢支持!

微信二维码

请使用微信扫描二维码打赏。

支付宝二维码

请使用支付宝扫描二维码打赏。