0

我已经尝试调试这个问题几个小时并用谷歌搜索它,甚至找不到遇到同样问题的人。出于某种原因,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]]}}

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

我也试过:

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 数据而不重新组织它吗?(我需要遍历数据,即使将来数据文件发生变化)

非常感谢!

4

1 回答 1

2

JSON 对象(节点)未排序。它是json格式的定义。在 Java 中,JSON 节点被实现为 HashMap,而 HashMap 也没有排序。

如果你真的需要用 JSON 排序的东西,你需要使用 JSON Array。例子:

{
"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]]]
.
.
.
于 2013-07-17T11:58:03.050 回答