0

我有一个像这样格式化的 json 文件(只有实际文件没有空格):

{
    "main": [
        {
            "sections": [
                "sec1",
                "sec2"
            ],
            "title": "Section List 1"
        },
        {
            "sections": [
                "sec3",
                "sec4",
                "sec5"
            ],
            "title": "Section List 2"
        }
    ],
    "sections": {
        "sec1": {
            "products": [
                "prod1",
                "prod2"
            ]
        },
        "sec2": {
            "products": [
                "prod3"
            ]
        }
    },
    "products": {
        "prod1": {
            "url": "url1.gif",
            "title": "Product 1"
        },
        "prod2": {
            "url": "url2.gif",
            "title": "Product 2"
        },
        "prod3": {
            "url": "url3.gif",
            "title": "Product 3"
        }
    }
}

当我尝试将该文件的数据发送到 JSONObject 时,正在加载的 JSONObject 仅包含顶部列表中的最终对象,在本例中为“产品”。现在,我加载 JSONObject 的代码是这样的:

JSONObject dlResult = new JSONObject(new Scanner(cnxn.getInputStream()).nextLine());

我还尝试先将数据存储在 String 中并将其提供给 JSONObject 构造函数,我尝试先将该 String 提供给 JSONTokener,然后将该标记器提供给 JSONObject 构造函数。String 和 JSONTokener 都包含整个文件,但是一旦将其放入 JSONObject 中,它总是一样的——“main”和“sections”被删除,只剩下“products”。

这是我的其余相关代码:

public class MapsListFragment extends ListFragment
{
    private static JSONObject mDlResult;
    private ArrayAdapter<MapInfo> mMapListAdapter = null;

    private class MapInfo
    {
        private String mText;
        private String mURL;

        MapInfo(String inText, String inURL)
        {
            mText = inText;
            mURL = inURL;
        }

        public String URL()
        {
            return mURL;
        }

        @Override
        public String toString()
        {
            return mText;
        }
    }

public class MapsListUpdater extends AsyncTask<String, String, JSONObject>
    {
        private static final String URL = "http://jsonURL/products.json";

        private Date lastUpdate;

        @Override
        protected JSONObject doInBackground(String... inObjects)
        {
            Date ifModifiedSince = lastUpdate;

            try 
            {
                HttpURLConnection cnxn = (HttpURLConnection) new URL(URL).openConnection();
                if (ifModifiedSince != null)
                    cnxn.setIfModifiedSince(ifModifiedSince.getTime());

                cnxn.connect();
                if (cnxn.getResponseCode() != HttpURLConnection.HTTP_NOT_MODIFIED)
                {
                    mDlResult = new JSONObject(new Scanner(cnxn.getInputStream()).nextLine());
                }

                cnxn.disconnect();
            } 
            catch (Exception e) 
            {
                boolean check = true;
            }

            return mDlResult;
        }

        @Override
        protected void onPostExecute(JSONObject result)
        {
            super.onPostExecute(result);
            try 
            {
                // This is really ugly and brute-forcey, but it's the only way I could get JSONObjects populated with ALL the data!
                /*String[] tryThis = mDlResult.split(",\"sections\":");
                tryThis[0] += '}';
                tryThis[1] = tryThis[1].substring(0, tryThis[1].indexOf("]}},\"products\":"));
                tryThis[1] = "{\"sections\":" + tryThis[1] + "]}}}";*/

                JSONObject mainObj = mDlResult.getJSONObject("main");//new JSONObject(mDlResult);

                JSONArray mainArray = mainObj.getJSONArray("main");

                Vector<String> titles = new Vector<String>();

                mMapListAdapter.clear();

                for (int i = 0; i < mainArray.length(); i++)
                {
                    JSONObject object = mainArray.getJSONObject(i);

                    String title = object.getString("title");

                    if(!titles.contains(title))
                    {
                        titles.add(title);
                        mMapListAdapter.add(new MapInfo(object.getString("title"), null));
                    }
                }
            }
            catch (Exception e) 
            {
                boolean check = true;
            } 
        }
    }
}
4

2 回答 2

1

我现在觉得有点傻。事实证明,JSONObject 得到的正是我所要求的……只有其中的对象被重新排序,所以我在调试器中看不到前 2 个对象!

于 2013-02-11T21:12:44.740 回答
0

尝试将其读入JSONArray并让我知道您的进展情况:

JSONArray dlResult = new JSONArray(cnxn.getInputStream());

于 2013-02-01T00:03:14.937 回答