gpt4 book ai didi

java - 为什么 JSONTokener/Object 不断随机化我的 json 数据?

转载 作者:行者123 更新时间:2023-12-02 06:53:37 25 4
gpt4 key购买 nike

我已经尝试调试这个问题几个小时了,并用谷歌搜索了它,甚至找不到遇到同样问题的人。由于某种原因 JSONObject/JSONTokener 正在做一些奇怪的事情。

以下代码:

JSONObject jsonObj = (JSONObject) new JSONTokener(sourceJson).nextValue();

Log.d(" ", sourceJson + "\n");
Log.d(" ", jsonObj.toString());

产生此输出:

{
"2012":{
"federal":[[10822,0.15],[42707,0.22],[85414,0.26],[132406,0.29]],
"provincial":{
"AB":[[17282,0.1]],
"BC":[[11354,0.0506],[37013,0.077],[74028,0.105],[84993,0.1229],[103205,0.147]],
"MB":[[8634,0.108],[31000,0.1275],[67000,0.174]],
"NB":[[9203,0.091],[38190,0.121],[76380,0.124],[124178,0.143]],
"NL":[[8237,0.077],[32893,0.125],[65785,0.133]],
"NS":[[8481,0.0879],[29590,0.1495],[59180,0.1667],[93000,0.175],[150000,0.21]],
"NT":[[13280,0.059],[38679,0.086],[77360,0.122],[125771,0.1405]],
"NU":[[12211,0.04],[40721,0.07],[81442,0.09],[132406,0.115]],
"ON":[[9405,0.0505],[39020,0.0915],[78043,0.1116],[500000,0.1216]],
"PE":[[7708,0.098],[31984,0.138],[63969,0.167]],
"QC":[[10925,0.16],[40100,0.2],[80200,0.24]],
"SK":[[14942,0.11],[42065,0.13],[120185,0.15]],
"YT":[[10822,0.0704],[42707,0.0968],[85414,0.1144],[132406,0.1276]]
}
}
}

{"2012":{"provincial":{"ON":[[9405,0.0505],[39020,0.0915],[78043,0.1116],[500000,0.1216]],"AB":[[17282,0.1]],"BC":[[11354,0.0506],[37013,0.077],[74028,0.105],[84993,0.1229],[103205,0.147]],"NL":[[8237,0.077],[32893,0.125],[65785,0.133]],"QC":[[10925,0.16],[40100,0.2],[80200,0.24]],"NU":[[12211,0.04],[40721,0.07],[81442,0.09],[132406,0.115]],"SK":[[14942,0.11],[42065,0.13],[120185,0.15]],"PE":[[7708,0.098],[31984,0.138],[63969,0.167]],"NT":[[13280,0.059],[38679,0.086],[77360,0.122],[125771,0.1405]],"NS":[[8481,0.0879],[29590,0.1495],[59180,0.1667],[93000,0.175],[150000,0.21]],"YT":[[10822,0.0704],[42707,0.0968],[85414,0.1144],[132406,0.1276]],"NB":[[9203,0.091],[38190,0.121],[76380,0.124],[124178,0.143]],"MB":[[8634,0.108],[31000,0.1275],[67000,0.174]]},"federal":[[10822,0.15],[42707,0.22],[85414,0.26],[132406,0.29]]}}

上面格式良好的数据与我用来填充 sourceJson 的 data.json 文件相同,而且很长的一行已经神秘地重新排列了省份的顺序(但不是每个省份中的数据)省的二维数组)。

我也尝试过:

JSONObject jsonObj = new JSONObject(sourceJson); // same problem

JSONTokener jsonTk = new JSONTokener(sourceJson);
Log.d(" ", jsonTk.toString()); // result = " at character 0 of " + sourceJson;

Log.d(" ", jsonTk.nextValue().toString()); // output same as original problem

我还尝试使用断点来跟踪对 nextValue() 的调用,但找不到问题的原因。该行为表明 JSONObject(String) 构造函数可能会调用 nextValue(),因为这两种方法给出相同的输出。

所以问题是:有谁知道为什么 JSONObject/JSONTokener.nextValue() 会这样?而且,更重要的是,您知道如何让它在不重新组织的情况下解析 JSON 数据吗? (我需要迭代数据,即使数据文件将来发生变化)

非常感谢!

最佳答案

JSON 对象(节点)未排序。这是json格式的定义。在Java中,JSON节点实现为HashMap,并且HashMap也不是有序的。

如果您确实需要以 JSON 形式订购某些内容,则需要使用 JSON 数组。示例:

{
"2012":{
"federal":[[10822,0.15],[42707,0.22],[85414,0.26],[132406,0.29]],
"provincial":{
["code":"AB", data:[[17282,0.1]]],
["code":"BC", data:[[11354,0.0506],[37013,0.077],[74028,0.105],[84993,0.1229],[103205,0.147]]],
["code":"MB", data:[[8634,0.108],[31000,0.1275],[67000,0.174]]]
.
.
.

关于java - 为什么 JSONTokener/Object 不断随机化我的 json 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17698919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com