【问题标题】:Reading JSON double dimensional array in Java在Java中读取JSON双维数​​组
【发布时间】:2013-03-14 14:13:22
【问题描述】:

每个news 条目都有三个 东西:titlecontentdate

条目是从数据库中检索的,我想在我的应用程序中使用 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"
      }
   }
]

【问题讨论】:

  • 首先,创建一个名为 News 的类,其中包含 3 个字段:标题、内容和日期。
  • 阅读有关如何使用 java json 解析器的文档。

标签: java android json


【解决方案1】:

您的 JSON 字符串是 JSONObjectJSONArray,然后包含一个称为“新闻”的内部 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 */
}

【讨论】:

  • @JonathanHugh 很高兴您能够找到问题的答案。请记住,如果给出的答案满足您的需求,您应该通过单击投票箭头下方的复选标记来接受该答案,以便将来查看此问题的人能够知道哪个答案解决了问题。
【解决方案2】:

首先,您的 JSON 结构并不理想。您有一个对象数组,每个对象中都有一个对象。但是,您可以这样阅读:

JSONArray jsonArray = new JSONArray (jsonString);
int arrayLength = jsonArray.length ();

for (int counter = 0; counter < arrayLength; counter ++) {
    JSONObject thisJson = jsonArray.getJSONObject (counter);

    // we finally get to the proper object
    thisJson = thisJson.getJSONObject ("news");

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

}

但是!

如果您将 JSON 更改为如下所示,您会做得更好:

[
    {
        "title": "5th title",
        "content": "5th content",
        "date": "1363197493"
    },
    {
        "title": "4th title",
        "content": "4th content",
        "date": "1363197454"
    }
]

那么,你可以这样解析:

JSONArray jsonArray = new JSONArray (jsonString);
int arrayLength = jsonArray.length ();

for (int counter = 0; counter < arrayLength; counter ++) {
        // we don't need to look for a named object any more
    JSONObject thisJson = jsonArray.getJSONObject (counter);    

    String title = thisJson.getString ("title");
    String content = thisJson.getString ("content");
    String date = thisJson.getString ("date");
}

【讨论】:

  • 你得到错误的日期是一个字符串,你不会使用 getLong() 得到它,检查引号
  • “你的 json 不好”是意见。该数组可能包含除“新闻”之外的其他对象,因此 json 的格式实际上是非常具有描述性的。
  • @323go 他实际上在标题中说这是一个二维数组,在问题的第一行中他说“每个新闻都有三件事:标题、内容和日期”。我觉得他不是很懂 JSON 数组和对象的语法,所以我试着举个例子。
  • 我认为 Shade 是对的,我的 json 不如他建议的那么好,这只是因为我使用 php 对其进行编码,这就是我得到的,我无法像你一样做到做了,但我会试试的。无论如何,谢谢你的帮助! :)
  • 没问题。您可以针对生成 JSON 的 PHP 代码提出另一个问题。
猜你喜欢
  • 1970-01-01
  • 2012-05-11
  • 2016-02-02
  • 2019-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-26
  • 1970-01-01
相关资源
最近更新 更多