加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

在Java中读取JSON二维数组

发布时间:2020-12-14 05:44:03 所属栏目:Java 来源:网络整理
导读:每个新闻条目都有三个内容:标题,内容和日期. 从数据库中检索条目,我想在我的应用程序中使用JSONObject和JSONArray读取它们.但是,我不知道如何使用这些类. 这是我的JSON字符串: [ { "news":{ "title":"5th title","content":"5th content","date":"13631974
每个新闻条目都有三个内容:标题,内容和日期.

从数据库中检索条目,我想在我的应用程序中使用JSONObject和JSONArray读取它们.但是,我不知道如何使用这些类.

这是我的JSON字符串:

[
   {
      "news":{
         "title":"5th title","content":"5th content","date":"1363197493"
      }
   },{
      "news":{
         "title":"4th title","content":"4th content","date":"1363197454"
      }
   },{
      "news":{
         "title":"3rd title","content":"3rd content","date":"1363197443"
      }
   },{
      "news":{
         "title":"2nd title","content":"2nd content","date":"1363197409"
      }
   },{
      "news":{
         "title":"1st title","content":"1st content","date":"1363197399"
      }
   }
]

解决方法

您的JSON字符串是JSONObject的JSONArray,然后包含一个名为“news”的内部JSONObject.

试试这个来解析它:

JSONArray array = new JSONArray(jsonString);

for(int i = 0; i < array.length(); i++) {
    JSONObject obj = array.getJSONObject(i);
    JSONObject innerObject = obj.getJSONObject("news");

    String title = innerObject.getString("title");
    String content = innerObject.getString("content");
    String date = innerObject.getString("date");

    /* Use your title,content,and date variables here */
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读