解析json新闻源数组android

时间:2012-11-15 16:57:47

标签: java android

我有这种格式的bbc的json feed

{
  "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");并且它给我一个路径错误(注意

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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");

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