Json被排列成错误的顺序

时间:2013-12-15 15:52:43

标签: php android mysql json

我正在开发一个从MySQL数据库中检索信息的Android应用程序。

Android应用程序发布到PHP REST Web服务,Web服务正在返回JSON数据。

我目前要做的是按字母顺序获取MySQL服务器上的数据库列表。当JSON打印到logcat时,它似乎是正确的顺序,但是当我在android中的JSONObject上调用json.names()时,它的数据库顺序错误。

例如,logcat可能会将其显示为db1,db2,db3,db4,但是当从json.names()返回数组时,它似乎是随机顺序。例如db4,db1,db3,db2。

这有什么特别的原因,我怎么能阻止这种情况发生。

感谢您提供的任何帮助

以下是处理JSON的代码

public void processConnectDBResult(IConnectDB iConnectDb, JSONObject json)
    {
        ArrayList<String> databases = new ArrayList<String>();
        ArrayList<String> tables = null;
        HashMap<String, List<String>> dbAndTables = new HashMap<String, List<String>>();

        try
        {
            //Retrieve the array of the databases
            JSONArray databasesJson = json.names();

            for (int i = 0; i < databasesJson.length(); i++)
            {
                databases.add(databasesJson.getString(i));
                //Retrieve the tables array from the database array
                JSONArray tablesJson = json.getJSONArray(databasesJson.getString(i));

                tables = new ArrayList<String>();
                for (int j = 0; j < tablesJson.length(); j++)
                {
                    tables.add(tablesJson.getString(j));
                }
                dbAndTables.put(databases.get(i), tables);
            }
            iConnectDb.processDatabaseRetrievalResult("SUCCESS", "", databases, dbAndTables);
        }
        catch (Exception ex)
        {

        }
    }

0 个答案:

没有答案