【问题标题】:Java Extract data from JSON ArrayJava 从 JSON 数组中提取数据
【发布时间】:2018-05-18 08:32:19
【问题描述】:

下面是json格式,我要提取动态键值对的数据。

{"data": [
         {"1000":"2018-03-10 00:00:00.0"},
         {"100031": "2018-03-15"}
         ]
}

这是我正在尝试的东西,但它不起作用并显示错误:

在输入中,我得到了 JSON 对象。

JSONArray lastdtArr =   json.getJSONArray("data");
int len = lastdtArr.length();
for(int i=0;i<len;i++) {
    JSONObject lastdtid = lastdtArr.getJSONObject(i);
    int id = lastdtid.getKey();
    String date = lastdtid.getValue();
    //Operations to perform
}

PS:我对 Java 完全陌生。我在互联网上找到的解决方案是 json 格式,其中键固定在键值对中。但在这种情况下,密钥始终是动态的。请让我知道我必须在循环中写什么来获取 id 和 date 中的值?

【问题讨论】:

  • 您应该看一看涵盖这个确切问题的数千个教程之一。
  • @Ben 确实。它可能是一个/meta,但即使我们的工作被定义和重新定义为帮助者数千次,来到这里寻求一些具有特定问题的教学也不是一件坏事,如How to Ask 页面中所述。最重要的是,无论您如何看待,大多数在这里寻求帮助的代码新手也需要一些教学:)。我认为分享原始知识不是一件坏事,正如一位杰出的科学家有一天所说:如果我给你知识,我不会失去任何东西,我只是通过你和我的相加创造新的东西:)
  • @xoxel 我不确定你想告诉我什么,抱歉。
  • 我不知道如何更好地解释自己,只是为了简历:ofc 他应该自己看,但这并不意味着他不应该帮助他。
  • 现在我明白了。他已经在 SO 上工作了近 6 年,并提出了一个问题,只需在此处的搜索栏中输入确切的标题,就会得到 40 个其他结果。提供建议的适用范围有限,而不是指出此处提出问题的基本原则(首先进行一些研究,使用搜索栏,向我们展示您尝试过的内容等)尚未实现。对于资深会员来说更是如此。

标签: java arrays json


【解决方案1】:

您需要使用 JSONArray 来提取数据,因为它是一个 OBJECT 数组,然后您可以使用 JSONObject 对其进行迭代以访问该数组中的每个对象,这是我几个月前写的快速指南:

What is a JSON object : (just to be clear) A .json file contains text-based representations of data structures and objects. A JSON object is delimited by "{" and "}" and represent a single object/structure inside a .json file.

JSONObject : It's a type representing a single JSON object in JAVA.

JSONArray : Seems obvious, but it's an array wich contains JSONObject elements, 
            i recommend you to use this to keep your objects packed into an array,
            this type is providing a lot of usefull methods aswell as JSONObject does,
            i'll list some of them below it's up to you to search for the others. 

JSONArray.getJSONObject(index) : Pass it an index and it will return the JSONObject stored 
                                 at this index.
JSONArray.put([value] | [index, value]) : This might be extremly straightforward but it's actually pretty 
                       usefull if you want to build a JSONArray, pass it a JSONObject 
                       or another common type (int, String, etc) and it will add it to 
                       your JSONArray. 
                       Depending on what you pass it you'll need an index aswell.

JSONObject.keys() : Returns an iterator of the String names in your JSONObject.

JSONObject.put(name, value) : Create a field in your JSONObject using the name you passed 
                              and assigning the value to it.

JSONObject.getString(name) : Pass the name of a field to it and it will return 
                             it's value as a String. As you may have guess already, there is a couple of those, not only getString().

剩下的看你自己了。

【讨论】:

  • 感谢 xoxel。如您所见,我没有字段名称。键是整数和动态的。因此我无法检索这些值。
猜你喜欢
  • 2014-07-05
  • 1970-01-01
  • 2015-02-02
  • 1970-01-01
  • 2020-04-09
  • 2014-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多