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

1.2.3版後 JSONObject.toJSONString回傳的字串不再依key值排序了 #359

Closed
limhi opened this issue Jun 24, 2015 · 6 comments
Closed

Comments

@limhi
Copy link

limhi commented Jun 24, 2015

即便使用了 SerializerFeature.SortField 亦得到錯誤結果
測試程式:

String str = "{\"Format\":\"json\",\"Name\":\"wwqqzz\",\"Para\":{\"myuser\":333,\"mynum\":\"222\", \"mylist\":[11,55,\"33\"]},\"Reserved\":{},\"Type\":\"request\",\"Version\":\"1.0\"}";
System.out.println("str = \t\t" + str);

JSONObject json = JSONObject.parseObject(str);

System.out.println("0.0 json  = \t" + JSONObject.toJSONString(json));
System.out.println("0.1 json  = \t" + JSONObject.toJSONString(json, SerializerFeature.SortField));

使用1.2.2版時輸出 => 正常

str =       {"Format":"json","Name":"wwqqzz","Para":{"myuser":333,"mynum":"222", "mylist":[11,55,"33"]},"Reserved":{},"Type":"request","Version":"1.0"}
0.0 json  =     {"Format":"json","Name":"wwqqzz","Para":{"mylist":[11,55,"33"],"mynum":"222","myuser":333},"Reserved":{},"Type":"request","Version":"1.0"}
0.1 json  =     {"Format":"json","Name":"wwqqzz","Para":{"mylist":[11,55,"33"],"mynum":"222","myuser":333},"Reserved":{},"Type":"request","Version":"1.0"}

使用1.2.3~1.2.6版時輸出 => 亂序

str =       {"Format":"json","Name":"wwqqzz","Para":{"myuser":333,"mynum":"222", "mylist":[11,55,"33"]},"Reserved":{},"Type":"request","Version":"1.0"}
0.0 json  =     {"Type":"request","Format":"json","Para":{"mynum":"222","mylist":[11,55,"33"],"myuser":333},"Version":"1.0","Name":"wwqqzz","Reserved":{}}
0.1 json  =     {"Type":"request","Format":"json","Para":{"mynum":"222","mylist":[11,55,"33"],"myuser":333},"Version":"1.0","Name":"wwqqzz","Reserved":{}}
@FkinH
Copy link

FkinH commented Jun 24, 2015

try this.

@JSONType(orders = {})

@limhi
Copy link
Author

limhi commented Jun 25, 2015

在我這個案例中,並不存在需要反序列化的java物件,也就沒有地方可以放annotation

@wenshao wenshao added this to the 1.2.10 milestone Apr 13, 2016
@wenshao
Copy link
Member

wenshao commented Apr 15, 2016

原来是将其他类型的Map转为TreeMap再序列化,这样开销较大,如果需要,用ValueFilter将Map转成TreeMap。

@wenshao wenshao closed this as completed Apr 15, 2016
@wenshao wenshao removed this from the 1.2.10 milestone Apr 23, 2016
@limhi
Copy link
Author

limhi commented May 4, 2016

wenshao提供的ValueFilter方式,僅能調整value物件
但是key本身在輸出的json 字串中,依然是無序的

@Arisono
Copy link

Arisono commented Mar 10, 2017

LinkedHashMap<String, Object> root=JSON.parseObject(map.toString(),new TypeReference<LinkedHashMap<String, Object>>(){} ); 这样对于单map对象可以解决顺序问题,嵌套的map还没有尝试,不过这可以是一种解决方法,希望对你有帮助@limhi

@sesames
Copy link

sesames commented Jun 8, 2018

That is good

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

5 participants