Java从JSON数组中提取数据

Java从JSON数组中提取数据,java,arrays,json,Java,Arrays,Json,下面是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(in

下面是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
}
JSONArray lastdtArr=json.getJSONArray(“数据”);
int len=lastdtArr.length();

对于(int i=0;i您需要使用JSONArray提取数据,因为它是一个对象数组,然后您可以使用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().

剩下的就看你了。

你应该看一看关于这个确切问题的数千本教程中的一本。@Ben确实如此。它可能是a/meta,但即使我们的工作被定义为并被重新定义为数千次的助手,来这里寻求一些关于p中描述的特定问题的教学也不是一件坏事最重要的是,大多数在这里搜索帮助的代码新手也需要一些指导,不管你怎么看:)。我认为分享原始知识并不是一件坏事,正如一位杰出的科学家有一天所说:如果我给你知识,我不会失去任何东西,我只是从你和我的知识中创造了一些新的东西:)@xoxel我不知道你想告诉我什么,对不起。我不知道如何更好地解释自己,就为了简历:ofc他应该自己看,但这并不意味着我们不应该帮他。现在我明白了。他已经这样做了近6年,并提出了一个问题,只要在搜索栏中输入准确的标题,就可以得到40个其他结果。给出建议的适用范围有一个限制,而不是指出这里提问的基本要求(先做一些研究,使用搜索栏,向我们展示你的尝试,等等)没有实现。对于一位资深会员来说更是如此。谢谢xoxel。如你所见,我没有字段的名称。键是整数和动态的。因此,我无法检索这些值。