cjson的encode后的字符的顺序问题

161 views
Skip to first unread message

lhmwzy

unread,
Oct 23, 2012, 1:58:07 AM10/23/12
to open...@googlegroups.com
有如下代码:
location /echo1 {
content_by_lua '
local str = {y=123,b=234,c=456}
local cjson = require "cjson"
local out = cjson.encode(str)
ngx.say(out)
';
}

访问http://xxx/echo1会输出
{"y":123,"c":456,"b":234}

这个正常吗?
为啥不是按原来的{"y":123,"b":234,"c":456} 输出?

Lance

unread,
Oct 23, 2012, 2:20:22 AM10/23/12
to open...@googlegroups.com
似乎是lua hash table不保证顺序稳定
Lance


2012/10/23 lhmwzy <lhm...@gmail.com>

lhmwzy

unread,
Oct 23, 2012, 2:21:19 AM10/23/12
to open...@googlegroups.com
那如何让它保持原来的顺序?

G_will

unread,
Oct 23, 2012, 2:20:56 AM10/23/12
to open...@googlegroups.com
正常,大多数语言中的hash table都是无序或者说是顺序不确定的。
只不过json恰好是字符串有顺序。
需要用到顺序的地方就得用列表或者说数组。
Reply all
Reply to author
Forward
0 new messages