Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What is the sorting basis for key values after decode? #95

Open
NoWait126 opened this issue Aug 19, 2023 · 1 comment
Open

What is the sorting basis for key values after decode? #95

NoWait126 opened this issue Aug 19, 2023 · 1 comment

Comments

@NoWait126
Copy link

Here is the content of json file

{
    "1": "1",
    "2": "2",
    "3": "3",
    "4": "4"
}

here is my code

local cjson = require "cjson"
local function load_file(user_conf_file)
    local f = io.open(user_conf_file)
    if not f then
      return nil
    end
    local cont = f:read("*a")
    f:close()
    local fjson = cjson.decode(cont)
    for k, v in pairs(fjson) do
      print(k)
    end
    return true
end

The output order is different each time,such as1432, 3142 .
So what is the sorting basis for key values after decode? Is there a way to guarantee the original order?

@zhuizhuhaomeng
Copy link

luajit adds randomness to lua table, so the order in which the table is traversed is not fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants