Object cannot be converted to JSONArray

lihengsuda 2014-12-10 11:37:44
public static List<News> getListNews(String urlPath) throws Exception {
final List<News> mlists = new ArrayList<News>();
final byte[] data = readParse(urlPath);
final JSONArray array = new JSONArray(new String(data));
for (int i = 0; i < array.length(); i++) {
final JSONObject item = array.getJSONObject(i);
final String id = item.getString("id");
final String title = item.getString("title");
final String content = item.getString("content");
final String outline = item.getString("outline");
mlists.add(new News(id,title,content,outline));
}
return mlists;



json数据是这样的
{"news":[{"content":"一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一","id":1,"outline":"11111111111111111111111","title":"111111111"},{"content":"二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二","id":2,"outline":"222222222222222222222222222222222222222","title":"222222222222"},{"content":"三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三","id":3,"outline":"333333333333333333333333333333333333333333333333333333333333333","title":"33333333333333"}]}

...全文
307 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
lihengsuda 2014-12-10
  • 打赏
  • 举报
回复
[quote=引用 7 楼 u012374885 的回复:] org.json.JSONException:Value tttp of type java.lang.String cannot be converted to JSONObject at xxxxxx getNewsList
lihengsuda 2014-12-10
  • 打赏
  • 举报
回复
[quote=引用 7 楼 u012374885 的回复:]
	public static List<News> getListNews(String urlPath) throws Exception {
		List<News> mlists = new ArrayList<News>();
		JSONObject aJson = new JSONObject(urlPath);
		JSONArray array = aJson.optJSONArray("news");
		for (int i = 0; i < array.length(); i++) {
			JSONObject item = array.getJSONObject(i);
			String id = item.optString("id");
			String title = item.optString("title");
			String content = item.optString("content");
			String outline = item.optString("outline");
			mlists.add(new News(id, title, content, outline));
		}
		return mlists;
	}
又报异常 :org.json.JSONException:Value tttp of type java.lang.String cannot be converted to JSONObject
shkale 2014-12-10
  • 打赏
  • 举报
回复
我也去试试,谢谢
荔枝别闹了 2014-12-10
  • 打赏
  • 举报
回复
	public static List<News> getListNews(String urlPath) throws Exception {
		List<News> mlists = new ArrayList<News>();
		JSONObject aJson = new JSONObject(urlPath);
		JSONArray array = aJson.optJSONArray("news");
		for (int i = 0; i < array.length(); i++) {
			JSONObject item = array.getJSONObject(i);
			String id = item.optString("id");
			String title = item.optString("title");
			String content = item.optString("content");
			String outline = item.optString("outline");
			mlists.add(new News(id, title, content, outline));
		}
		return mlists;
	}
试试
lihengsuda 2014-12-10
  • 打赏
  • 举报
回复
引用 4 楼 u012374885 的回复:
那个Result是我自己,忘记替换了,你的应该是news

lihengsuda 2014-12-10
  • 打赏
  • 举报
回复
引用 3 楼 u012374885 的回复:
首先
JSONObject aJson = new JSONObject(urlPath);
然后
JSONArray array = aJson.optJSONArray("Result");
最后 把array放进你之前那个循环里
能帮我补下代码吗 我改了下 全是错
荔枝别闹了 2014-12-10
  • 打赏
  • 举报
回复
那个Result是我自己,忘记替换了,你的应该是news
荔枝别闹了 2014-12-10
  • 打赏
  • 举报
回复
首先
JSONObject aJson = new JSONObject(urlPath);
然后
JSONArray array = aJson.optJSONArray("Result");
最后 把array放进你之前那个循环里
lihengsuda 2014-12-10
  • 打赏
  • 举报
回复
引用 1 楼 u012374885 的回复:
首先,最外层不是数组,是个object,不能array来解
那应该怎么解析呢?
荔枝别闹了 2014-12-10
  • 打赏
  • 举报
回复
首先,最外层不是数组,是个object,不能array来解
lihengsuda 2014-12-10
  • 打赏
  • 举报
回复
引用 17 楼 u012374885 的回复:
[ 这样行不行
谢谢 我自己再看看把 麻烦你了
荔枝别闹了 2014-12-10
  • 打赏
  • 举报
回复
public static List<News> getListNews(String urlPath) throws Exception {
    List<News> mlists = new ArrayList<News>();
    byte[] data = readParse(urlPath);
    JSONObject aJson = new JSONObject(new String(data));
    JSONArray array = aJson.optJSONArray("news");
    for (int i = 0; i < array.length(); i++) {
        JSONObject item = array.optJSONObject(i);
        String id = item.optString("id");
        String title = item.optString("title");
        String content = item.optString("content");
        String outline = item.optString("outline");
        mlists.add(new News(id, title, content, outline));
    }
    return mlists;
}
这样行不行
lihengsuda 2014-12-10
  • 打赏
  • 举报
回复
引用 15 楼 u012374885 的回复:
。。。。 urlpath不是json数据啊,我以为是json,你得先用你的readparse得到json啊,最后解的是new string(data)这个json数据
我做了TAG JSON数据已经能读出来了 readParse(urlPath) 已经赋值给final byte[] data了 final byte[] data = readParse(urlPath); JSONArray array = new JSONArray(new String(data)) 我也不知道哪出问题了 你看私信 能加QQ聊吗
荔枝别闹了 2014-12-10
  • 打赏
  • 举报
回复
。。。。 urlpath不是json数据啊,我以为是json,你得先用你的readparse得到json啊,最后解的是new string(data)这个json数据
lihengsuda 2014-12-10
  • 打赏
  • 举报
回复
引用 11 楼 u012374885 的回复:
at哪里啊?
哪一行报错了,应该没有问题啊



报空指针的代码

public List<String> getData(){
try {
news = JsonParse.getListNews(urlPath);
} catch (Exception e) {
e.printStackTrace();
}
List<String> data = new ArrayList<String>();
for (int i = 1; i < news.size(); i++) {
data.add("id:"+news.get(i).getId());
data.add("title:"+news.get(i).getTitle());
data.add("content:"+news.get(i).getContent());
data.add("outline:"+news.get(i).getOutline());

}
return data;
}
lihengsuda 2014-12-10
  • 打赏
  • 举报
回复
引用 12 楼 u012374885 的回复:
你的urlpath是啥? readparse又是啥?
public static byte[] readParse(String urlPath) throws Exception { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] data = new byte[2048]; int len = 0; URL url = new URL(urlPath); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); InputStream inStream = conn.getInputStream(); while ((len = inStream.read(data)) != -1) { outStream.write(data, 0, len); } inStream.close(); return outStream.toByteArray(); } urlpath是我测试用的JSON网页 估计你打不开 我这里打开就是{"news":[{"content":"一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一","id":1,"outline":"11111111111111111111111","title":"111111111"},{"content":"二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二","id":2,"outline":"222222222222222222222222222222222222222","title":"222222222222"},{"content":"三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三三","id":3,"outline":"333333333333333333333333333333333333333333333333333333333333333","title":"33333333333333"}]}
荔枝别闹了 2014-12-10
  • 打赏
  • 举报
回复
你的urlpath是啥? readparse又是啥?
荔枝别闹了 2014-12-10
  • 打赏
  • 举报
回复
at哪里啊? 哪一行报错了,应该没有问题啊

80,362

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧