JSON字符串本次用的是网易的link.
将内容复制到JSON在线解析及格式化验证 link.可以更清晰的查看结构。
NewsClass取title、mtime、digest、url的内容
public class NewsClass {
String title;
String mtime;
String digest;
String url;
public NewsClass() {}
public NewsClass(String title, String mtime, String digest, String url) {
this.title = title;
this.mtime = mtime;
this.digest = digest;
this.url = url;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getMtime() {
return mtime;
}
public void setMtime(String mtime) {
this.mtime = mtime;
}
public String getDigest() {
return digest;
}
public void setDigest(String digest) {
this.digest = digest;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
遇到提示Unhandled exception: org.json.JSONException
选中代码,按快捷键Ctrl+Alt+T,选择6.try/catch即可
public class JsonClass {
public static List<NewsClass> JsonNews(String jsonnews){
//list数组承载返回值,jsonnews是带解析的串
List<NewsClass> list= new ArrayList<NewsClass>();
try {
JSONObject jsonObject = new JSONObject(jsonnews);
//数组节点
JSONArray jsonArray = jsonObject.getJSONArray("T1348647853363");
Log.i("LXH","jsonArray:"+jsonArray);
for (int i=0;i<jsonArray.length();i++){
//遍历获取数组内需要的内容
JSONObject jsonObject1 =jsonArray.getJSONObject(i);
NewsClass newsClass = new NewsClass(
jsonObject1.getString("title"),jsonObject1.getString("mtime"),
jsonObject1.getString("digest"),jsonObject1.getString("url")
);
list.add(newsClass);
}
} catch (JSONException e) {
e.printStackTrace();
}
return list;
}
}
这里的是用简单的部分json字符串,也可以搭配httpclient等获取HTTP数据
public class MainActivity extends AppCompatActivity {
List<NewsClass> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String data;
data ="{\n" +
" \"T1348647853363\":[\n" +
" {\n" +
" \"sourceId\":\"T1348647853363\",\n" +
" \"template\":\"normal1\",\n" +
" \"riskLevel\":0,\n" +
" \"upTimes\":1125,\n" +
" \"lmodify\":\"2021-07-13 17:06:44\",\n" +
" \"source\":\"澎湃新闻\",\n" +
" \"postid\":\"GEPQR3QR0514R9P4\",\n" +
" \"title\":\"郭新振被拐案嫌犯家乡村主任:他好吃懒做 有犯罪前科\",\n" +
" \"mtime\":\"2021-07-13 17:06:44\",\n" +
" \"hasImg\":1,\n" +
" \"topicid\":\"00019B3E\",\n" +
" \"topic_background\":\"http://cms-bucket.ws.126.net/2020/0422/1e44993bp00q966r20011c000u000a0c.png\",\n" +
" \"digest\":\"《失孤》迎来了大结局!7月12日,据公安部新闻发布会通报,电影《失孤》被拐儿童原型郭新振已经找到,呼某、唐某两名犯罪嫌疑人被抓获。澎湃新闻(www.thepap\",\n" +
" \"boardid\":\"dy_wemedia_bbs\",\n" +
" \"alias\":\"Top News\",\n" +
" \"hasAD\":1,\n" +
" \"imgsrc\":\"http://cms-bucket.ws.126.net/2021/0713/848e889fj00qw68uf0017c0009c0070c.jpg\",\n" +
" \"ptime\":\"2021-07-13 15:01:54\",\n" +
" \"daynum\":\"18821\",\n" +
" \"extraShowFields\":\"[{\\\"title\\\":\\\"\\\",\\\"showDocid\\\":\\\"00019B3E_GEPQR3QR0514R9P4_1\\\"}]\",\n" +
" \"hasHead\":1,\n" +
" \"order\":1,\n" +
" \"votecount\":1125,\n" +
" \"hasCover\":false,\n" +
" \"docid\":\"GEPQR3QR0514R9P4\",\n" +
" \"tname\":\"头条\",\n" +
" \"url_3w\":\"\",\n" +
" \"priority\":100,\n" +
" \"downTimes\":12,\n" +
" \"url\":\"\",\n" +
" \"quality\":80,\n" +
" \"commentStatus\":1,\n" +
" \"ads\":[\n" +
" {\n" +
" \"subtitle\":\"\",\n" +
" \"skipType\":\"photoset\",\n" +
" \"skipID\":\"00AO0001|2310991\",\n" +
" \"tag\":\"photoset\",\n" +
" \"title\":\"总统就职仪式即将开始 国会大厦秩序井然\",\n" +
" \"imgsrc\":\"http://pic-bucket.ws.126.net/photo/0001/2021-01-20/G0QOK7OQ00AO0001NOS.jpg\",\n" +
" \"url\":\"00AO0001|2310991\"\n" +
" },\n" +
" {\n" +
" \"subtitle\":\"\",\n" +
" \"skipType\":\"photoset\",\n" +
" \"skipID\":\"00AO0001|2310990\",\n" +
" \"tag\":\"photoset\",\n" +
" \"title\":\"特朗普发表卸任演说\",\n" +
" \"imgsrc\":\"http://pic-bucket.ws.126.net/photo/0001/2021-01-20/G0QNNVD500AO0001NOS.jpg\",\n" +
" \"url\":\"00AO0001|2310990\"\n" +
" },\n" +
" {\n" +
" \"subtitle\":\"\",\n" +
" \"skipType\":\"photoset\",\n" +
" \"skipID\":\"00AO0001|2310980\",\n" +
" \"tag\":\"photoset\",\n" +
" \"title\":\"回顾拜登78岁人生:从法律博士到最高龄总统\",\n" +
" \"imgsrc\":\"http://cms-bucket.ws.126.net/2021/0120/5cc628baj00qn8ldt0064c000ak0057c.jpg\",\n" +
" \"url\":\"00AO0001|2310980\"\n" +
" },\n" +
" {\n" +
" \"subtitle\":\"\",\n" +
" \"skipType\":\"photoset\",\n" +
" \"skipID\":\"00AO0001|2310989\",\n" +
" \"tag\":\"photoset\",\n" +
" \"title\":\"特朗普离开白宫\",\n" +
" \"imgsrc\":\"http://cms-bucket.ws.126.net/2021/0120/82752231j00qn8lfu004oc000550038c.jpg\",\n" +
" \"url\":\"00AO0001|2310989\"\n" +
" },\n" +
" {\n" +
" \"subtitle\":\"\",\n" +
" \"skipType\":\"photoset\",\n" +
" \"skipID\":\"00AP0001|2310630\",\n" +
" \"tag\":\"photoset\",\n" +
" \"title\":\"14岁男孩身高221.03厘米 成世界最高青少年\",\n" +
" \"imgsrc\":\"http://cms-bucket.ws.126.net/2020/1118/8dae7151j00qjz7po0035c000s600e3c.jpg\",\n" +
" \"url\":\"00AP0001|2310630\"\n" +
" }\n" +
" ],\n" +
" \"ename\":\"iosnews\",\n" +
" \"replyCount\":1154,\n" +
" \"ltitle\":\"郭新振被拐案嫌犯家乡村主任:他好吃懒做 有犯罪前科\",\n" +
" \"hasIcon\":true,\n" +
" \"subtitle\":\"\",\n" +
" \"cid\":\"C1348646712614\"\n" +
" },\n" +
" {\n" +
" \"sourceId\":\"T1348647853363\",\n" +
" \"riskLevel\":0,\n" +
" \"upTimes\":17586,\n" +
" \"lmodify\":\"2021-07-13 15:10:52\",\n" +
" \"source\":\"方圆\",\n" +
" \"postid\":\"GEPLR3CK0512BEVO\",\n" +
" \"title\":\"男子收舅舅1300万创业资金 找患癌\\\"杀手\\\"谋害舅舅\",\n" +
" \"mtime\":\"2021-07-13 15:10:52\",\n" +
" \"topicid\":\"00019B3E\",\n" +
" \"digest\":\"(图片来源:摄图网)杀手“血狼”再次给赵勇发微信时,威胁他,限期三天,必须离开深圳,若是不听劝,后果会很严重。赵勇回复他说:你就成全武俊,快点动手吧。我们欠了那\",\n" +
" \"boardid\":\"dy_wemedia_bbs\",\n" +
" \"imgsrc\":\"http://cms-bucket.ws.126.net/2021/0713/9c143f69j00qw67hk000vc000s600e3c.jpg\",\n" +
" \"ptime\":\"2021-07-13 14:34:49\",\n" +
" \"daynum\":\"18821\",\n" +
" \"extraShowFields\":\"[{\\\"title\\\":\\\"\\\",\\\"showDocid\\\":\\\"00019B3E_GEPLR3CK0512BEVO_1\\\"}]\",\n" +
" \"votecount\":17586,\n" +
" \"docid\":\"GEPLR3CK0512BEVO\",\n" +
" \"url_3w\":\"\",\n" +
" \"priority\":100,\n" +
" \"downTimes\":194,\n" +
" \"url\":\"\",\n" +
" \"quality\":80,\n" +
" \"commentStatus\":1,\n" +
" \"replyCount\":17841,\n" +
" \"ltitle\":\"男子收舅舅1300万创业资金 找患癌\\\"杀手\\\"谋害舅舅\",\n" +
" \"subtitle\":\"\"\n" +
" }]}";
list=JsonClass.JsonNews(data);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/lvJson"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
newsshow.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/tvMtime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/tvDigest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
将得到的数组用listview展示
ListView lvJson;
List<Map<String,Object>> listdata;
lvJson=findViewById(R.id.lvJson);
listdata = new ArrayList<Map<String,Object>>();
for(int i=0;i<list.size();i++){
Map<String,Object> item = new HashMap<String,Object>();
item.put("title",list.get(i).getTitle());
item.put("mtime",list.get(i).getMtime());
item.put("digest",list.get(i).getDigest());
item.put("url",list.get(i).getUrl());
listdata.add(item);
}
SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(),
listdata, R.layout.newsshow,
new String[]{"title","mtime","digest"},
new int[]{R.id.tvTitle,R.id.tvMtime,R.id.tvDigest});
lvJson.setAdapter(adapter);