Android 无法将gorg.Json.JSONObject中的Json parsin转换为JSONArray
我的json是这样的,我想解析新闻项以获得标题、url、内容、feelabel、url和feedlabelAndroid 无法将gorg.Json.JSONObject中的Json parsin转换为JSONArray,android,json,jsonparser,Android,Json,Jsonparser,我的json是这样的,我想解析新闻项以获得标题、url、内容、feelabel、url和feedlabel { "appnews": { "appid": 466560, "newsitems": [ { "gid": "2284879949551103234", "title": "These are the top 100 Steam games of 2017", "url": "htt
{
"appnews": {
"appid": 466560,
"newsitems": [
{
"gid": "2284879949551103234",
"title": "These are the top 100 Steam games of 2017",
"url": "http://store.steampowered.com/news/externalpost/rps/2284879949551103234",
"is_external_url": true,
"author": "contact@rockpapershotgun.com (Alec Meer)",
"contents": "Another year over, a new one just begun, which means, impossibly, <em>even more games.</em> But what about last year? Which were the games that most people were buying and, more importantly, playing? As is now something of a tradition, Valve have let slip a big ol’ breakdown of the most successful titl...",
"feedlabel": "Rock, Paper, Shotgun",
"date": 1514919601,
"feedname": "rps",
"feed_type": 0,
"appid": 550
},
{
"gid": "2284879949546841782",
"title": "ShiroGames - Bye bye 2017 and WELCOME 2018 !!!",
"url": "http://store.steampowered.com/news/externalpost/steam_community_announcements/2284879949546841782",
"is_external_url": true,
"author": "Lord_brioche",
"contents": "Hello Northgardians, As the end of 2017 is upon us, we wanted to share with you a few thoughts. What a year! At the beginning of 2017, Northgard was about to start its 6-months Early Access period. At the time, the game was supposed to be a fun, solo strategy game with 4 clans and ShiroGames was onl...",
"feedlabel": "Community Announcements",
"date": 1514818282,
"feedname": "steam_community_announcements",
"feed_type": 1,
"appid": 466560
},
{
"gid": "2284879949521363459",
"title": "Best PC games of 2017",
"url": "http://store.steampowered.com/news/externalpost/rps/2284879949521363459",
"is_external_url": true,
"author": "contact@rockpapershotgun.com (RPS)",
"contents": "The calendar’s doors have been opened and the games inside have been eaten. But fear not, latecomer – we’ve reconstructed the list in this single post for easy re-consumption. Click on to discover the best games of 2017. <a href=\"https://www.rockpapershotgun.com/2017/12/25/best-pc-games-of-2017/#more-503951\" class=\"more-link\">(more…)</a> ",
"feedlabel": "Rock, Paper, Shotgun",
"date": 1514206814,
"feedname": "rps",
"feed_type": 0,
"appid": 240720
}
]
,
"count": 57
}
}
{
“appnews”:{
“appid”:466560,
“新闻项目”:[
{
“gid”:“2284879949551103234”,
“标题”:“这是2017年蒸汽游戏100强”,
“url”:”http://store.steampowered.com/news/externalpost/rps/2284879949551103234",
“是外部url”:是,
“作者”:contact@rockpapershotgun.com(亚历克·米尔)“,
“内容”:“又一年过去了,新的一年刚刚开始,这意味着,不可能有更多的游戏。但是,去年呢?大多数人都在购买哪些游戏,更重要的是,玩哪些游戏?正如现在的一种传统,Valve让最成功的游戏崩溃了……”,
“feedlabel”:“岩石、纸张、猎枪”,
“日期”:1514919601,
“feedname”:“rps”,
“馈送类型”:0,
“appid”:550
},
{
“gid”:“2284879949546841782”,
“标题”:“ShiroGames-再见2017,欢迎2018!!!”,
“url”:”http://store.steampowered.com/news/externalpost/steam_community_announcements/2284879949546841782",
“是外部url”:是,
“作者”:“布里奥什勋爵”,
“内容”:“Northgard人你好,2017年底即将到来,我们想与大家分享一些想法。多么美好的一年啊!2017年初,Northgard即将开始其6个月的早期访问期。当时,该游戏被认为是一款有趣的单人战略游戏,有4个部族,ShiroGames只是……”,
“feedlabel”:“社区公告”,
“日期”:1514818282,
“feedname”:“steam_社区_公告”,
“饲料类型”:1,
“appid”:466560
},
{
“gid”:“2284879949521363459”,
“标题”:“2017年最佳PC游戏”,
“url”:”http://store.steampowered.com/news/externalpost/rps/2284879949521363459",
“是外部url”:是,
“作者”:contact@rockpapershotgun.com(RPS)“,
“内容”:“日历和游戏的门已经打开,里面的游戏已经被吃掉。但不要担心,迟到者和我们已经在这篇文章中重新构建了列表,以便于再次消费。单击以发现2017年最好的游戏。”,
“feedlabel”:“岩石、纸张、猎枪”,
“日期”:1514206814,
“feedname”:“rps”,
“馈送类型”:0,
“appid”:240720
}
]
,
“计数”:57
}
}
Json解析代码如下
@Override
protected Void doInBackground(Void... arg0) {
News news = new News();
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String url = "http://api.steampowered.com/ISteamNews/GetNewsForApp/v0002/?appid=466560&count=3&maxlength=300&format=json";
String jsonStr = sh.makeServiceCall(url);
;
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
JSONArray contacts = jsonObj.getJSONArray("appnews");
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
news.setTittle( c.getString("title"));
news.setContent(c.getString("content"));
news.setDate(c.getString("date"));
news.setFeed(c.getString("feedlabel"));
news.setUrl(c.getString("url"));
newsList.add(news);
}
} catch (final JSONException e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
}
});
}
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server",
Toast.LENGTH_LONG).show();
}
});
}
return null;
}
@覆盖
受保护的Void doInBackground(Void…arg0){
新闻=新新闻();
HttpHandler sh=新的HttpHandler();
//向url发出请求并获得响应
字符串url=”http://api.steampowered.com/ISteamNews/GetNewsForApp/v0002/?appid=466560&count=3&maxlength=300&format=json";
字符串jsonStr=sh.makeServiceCall(url);
;
if(jsonStr!=null){
试一试{
JSONObject jsonObj=新的JSONObject(jsonStr);
JSONArray contacts=jsonObj.getJSONArray(“appnews”);
对于(int i=0;i
我遇到了这样的错误,我想解析新闻项以获得标题、url、内容、feelabel、url和feedlabel,这是我在本教程中使用的
org.json.JSONException:Value{“appid”:466560,“newsitems”:[{“gid”:“2284879949551103234”,“title”:“这是2017年Steam百强游戏”,“url”:“http:\/\/store.steampowered.com\/news\/externalpost\/rps\/2284879949551103234”,“是不是外部url”:“真的”,作者“:”contact@rockpapershotgun.com(亚历克·米尔)“目录”:“又一年过去了,新的一年刚刚开始,这意味着,不可能有更多的游戏。但去年呢?大多数人都在购买哪些游戏,更重要的是,玩哪些游戏?正如现在的一个传统,Valve已经泄露了一个大的ol’;最成功的titl的崩溃…,“feedlabel”:“岩石、纸张、猎枪”、“日期”“:1514919601,”feedname:“rps”,“feed类型”:0,“appid”:550},{”gid:“2284879949546841782”,“标题:”ShiroGames-再见2017,欢迎2018!!!”,“url:“http:\/\/store.steampowered.com\/news\/externalpost\/steam\u community\u公告\/2284879949546841782”,“是外部url:”真的,“作者”:“Lord\u brioche”,“内容”:”“大家好,Northgard人,2017年底即将到来,我们想和大家分享一些想法。这是多么美好的一年啊!2017年初,Northgard即将开始其6个月的早期访问期。当时,该游戏本应是一款有趣的单人战略游戏,有4个部族,ShiroGames仅为……”,“feedlabel”:“社区公告”、“日期”“:1514818282,“feedname”:“steam_社区公告”,“feed_类型”:1,“appid”:466560},{“gid”:“2284879949521363459”,“标题”:“2017年最佳PC游戏”,“url”:“http:\/\/store.steampowere”
org.json.JSONException: Value {"appid":466560,"newsitems":[{"gid":"2284879949551103234","title":"These are the top 100 Steam games of 2017","url":"http:\/\/store.steampowered.com\/news\/externalpost\/rps\/2284879949551103234","is_external_url":true,"author":"contact@rockpapershotgun.com (Alec Meer)","contents":"Another year over, a new one just begun, which means, impossibly, <em>even more games.<\/em> But what about last year? Which were the games that most people were buying and, more importantly, playing? As is now something of a tradition, Valve have let slip a big ol’ breakdown of the most successful titl...","feedlabel":"Rock, Paper, Shotgun","date":1514919601,"feedname":"rps","feed_type":0,"appid":550},{"gid":"2284879949546841782","title":"ShiroGames - Bye bye 2017 and WELCOME 2018 !!!","url":"http:\/\/store.steampowered.com\/news\/externalpost\/steam_community_announcements\/2284879949546841782","is_external_url":true,"author":"Lord_brioche","contents":"Hello Northgardians, As the end of 2017 is upon us, we wanted to share with you a few thoughts. What a year! At the beginning of 2017, Northgard was about to start its 6-months Early Access period. At the time, the game was supposed to be a fun, solo strategy game with 4 clans and ShiroGames was onl...","feedlabel":"Community Announcements","date":1514818282,"feedname":"steam_community_announcements","feed_type":1,"appid":466560},{"gid":"2284879949521363459","title":"Best PC games of 2017","url":"http:\/\/store.steampowered.com\/news\/externalpost\/rps\/2284879949521363459","is_external_url":true,"author":"contact@rockpapershotgun.com (RPS)","contents":"The calendar’s doors have been opened and the games inside have been eaten. But fear not, latecomer – we’ve reconstructed the list in this single post for easy re-consumption. Click on to discover the best games of 2017. <a href=\"https:\/\/www.rockpapershotgun.com\/2017\/12\/25\/best-pc-games-of-2017\/#more-503951\" class=\"more-link\">(more…)<\/a> ","feedlabel":"Rock, Paper, Shotgun","date":1514206814,"feedname":"rps","feed_type":0,"appid":240720}],"count":57} at appnews of type org.json.JSONObject cannot be converted to JSONArray
// fetch appnews jsonobject
JSONArray news= jsonObj.getJSONObject("appnews");
// fetch the news items jsonarray which actually contains your data
JSONArray contacts = news.getJSONArray("newsitems");
JSONArray appNews= jsonObj.getJSONObject("appnews");
JSONArray contacts = appNews.getJSONArray("newsitems");
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
news.setTittle( c.getString("title"));
news.setContent(c.getString("contents"));
news.setDate(c.getString("date"));
news.setFeed(c.getString("feedlabel"));
news.setUrl(c.getString("url"));
newsList.add(news);
}
try
{
JSONObject jsonObj = new JSONObject(jsonStr);
JSONObject jsonObjAppnews = jsonObj.getString("appnews");
JSONArray contacts = jsonObjAppnews.getJSONArray("newsitems");
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
news.setTittle( c.getString("title"));
news.setContent(c.getString("contents"));
news.setDate(c.getString("date"));
news.setFeed(c.getString("feedlabel"));
news.setUrl(c.getString("url"));
newsList.add(news);
}
}
catch (final JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}