1

我有来自 bbc 的这种格式的 json 提要

{
  "name": "ticker",
  "entries": [
    {
      "headline": "text",
      "prompt": "LATEST",
      "isBreaking": "false",
      "mediaType": "Standard",
      "url": ""
    },
    {
      "headline": "text",
      "prompt": "LATEST",
      "isBreaking": "false",
      "mediaType": "Standard",
      "url": ""
    }, 
   etc...........

我的代码如下:

ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
JSONObject json = JSONfunctions.getJSONfromURL("http:/......");
try{
    JSONArray  item = json.getJSONArray("entries");
    for (int i = 0; i<item.length(); i++) {
        HashMap<String, String> map = new HashMap<String, String>();
        JSONObject e = item.getJSONObject(i);
        JSONObject title = e.JSONObject("headline");
        map.put("title", "Title:" + e.getString("headline");
    }
}

它给了我错误"java.lang.String cannot be converted to JSONObject"

我也试过省略JSONObject title = e.JSONObject("headline");,它给了我一个路径错误(注意

4

2 回答 2

0

你想要 e.getString("headline"),而不是 e.JSONObject

于 2012-11-15T17:01:03.567 回答
0

利用

JSONObject e = item.getJSONObject(i);
map.put("title", "Title:" + e.getString("headline");

代替

    JSONObject e = item.getJSONObject(i);
    JSONObject title = e.JSONObject("headline");
    map.put("title", "Title:" + e.getString("headline");

因为ejson 对象不包含任何其他 json 对象,它只包含键和相应的值

于 2012-11-15T17:07:07.980 回答