Java,将JsonObject放入JsonArray中,但JsonArray在for循环的末尾采用相同的值
我是Java新手,在这段代码中,我试图将JsonObect放在JsonArray中,但问题是JsonArray只接受最后一个值,并且在其中是重复的 您不需要理解所有代码,只需要理解有趣的部分 我已经将代码中感兴趣的部分放在了//*******字段中,在这里//******//code//**********结束,在这里//******//如果你想测试的话,我还放了json文件 就我而言,我尝试将我的值放入JsonObject列表中,然后将其放入JsonArray中,但我得到了相同的结果 我还尝试使用带有索引的哈希映射,但它仍然不起作用 如果您打印JsonObject变量“Alldate”,您可以看到它有不同的值,但是当我将它们放入JsonArray数组“ArrayBuckets”中时,您将看到JsonArray有相同的值,这是最后的值 我不明白,因为我的jsonObject和数组在同一个循环中 谢谢 代码如下:Java,将JsonObject放入JsonArray中,但JsonArray在for循环的末尾采用相同的值,java,arrays,json,loops,for-loop,Java,Arrays,Json,Loops,For Loop,我是Java新手,在这段代码中,我试图将JsonObect放在JsonArray中,但问题是JsonArray只接受最后一个值,并且在其中是重复的 您不需要理解所有代码,只需要理解有趣的部分 我已经将代码中感兴趣的部分放在了//*******字段中,在这里//******//code//**********结束,在这里//******//如果你想测试的话,我还放了json文件 就我而言,我尝试将我的值放入JsonObject列表中,然后将其放入JsonArray中,但我得到了相同的结果 我还尝试
package jsonbi.bi;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.simple.parser.JSONParser;
import com.google.gson.Gson;
public class JsonBi9 {
JSONParser parser = new JSONParser();
JSONObject json;
Object obj;
Map.Entry<String, JsonNode> field;
HashMap newmap = new HashMap();
JsonFactory factory = new JsonFactory();
private ObjectMapper mapper = new ObjectMapper();
Gson gson = new Gson();
JSONArray jsonArray = new JSONArray();
JSONObject Alldate = new JSONObject();
JSONObject BucketsObject = new JSONObject();
JSONObject Groupe = new JSONObject();
JSONObject Aggregations = new JSONObject();
JSONObject TheJson = new JSONObject();
JSONArray ArrayBuckets = new JSONArray();
Hashtable ht = new Hashtable();
int j =0;
public JsonBi9() {
try {
// here I am readind the json file
obj = parser.parse(new FileReader("myjson2.json"));
ObjectMapper mapper = new ObjectMapper(factory);
JsonNode rootNode = mapper.readTree(obj.toString());
Iterator<Map.Entry<String, JsonNode>> fieldsIterator = rootNode.getFields();
JSONObject jsonObject = new JSONObject(obj.toString());
// here I am reading the fields in the json fields
while (fieldsIterator.hasNext()) {
field = fieldsIterator.next();
newmap.put(field.getKey(), field.getValue());
if (field.getKey().equals("aggregations")) {
System.out.println("hugo");
JSONObject aggregations = jsonObject.getJSONObject("aggregations");
JSONObject groupe = aggregations.getJSONObject("groupe");
JSONArray buckets = groupe.getJSONArray("buckets");
for (int i11a = 0; i11a < buckets.length(); i11a++) {
JSONObject obj1bucket = buckets.getJSONObject(i11a);
JSONObject date11 = obj1bucket.getJSONObject("date");
JSONArray obj11bucket = date11.getJSONArray("buckets");
List<String> listSubbucket = new ArrayList<String>();
for (int i11 = 0; i11 < obj11bucket.length(); i11++) {
listSubbucket.add(obj11bucket.getJSONObject(i11).toString());
// System.out.println(obj11bucket.getJSONObject(i11).toString());
}
jsonArray = new JSONArray();
for (int i11 = 0; i11 < listSubbucket.size(); i11++) {
JSONObject SubBucket = new JSONObject();
SubBucket.put("buckets", listSubbucket.get(i11));
getdocument(obj1bucket, Alldate);
//******************************here ********************************//
// System.out.println(SubBucket);
Alldate.put("date", SubBucket);
ArrayBuckets.put(Alldate);
//*µµ if you want to print the jsonObject and array
// System.out.println(Alldate);
// System.out.println(ArrayBuckets);*
//*********************************end here ******************************//
} // end first for loop
} //end second for loop
Enumeration e = ht.elements();
while(e.hasMoreElements())
System.out.println(e.nextElement());
getdocument(groupe, Groupe);
Groupe.put("buckets", ArrayBuckets);
Aggregations.put("groupe", Groupe);
// System.out.println(Aggregations);
} // fin du if
else {
// System.out.println("Key: " + field.getKey() + "\tValue:"
// + field.getValue());
TheJson.put(field.getKey(), field.getValue());
} // fin du else
} // fin du while
// I put all the documents
TheJson.put("aggregations", Aggregations);
System.out.println(TheJson);
} catch (Exception e) {
e.printStackTrace();
}
}
public void getdocument(JSONObject obj1bucket, JSONObject Alldate) {
// get the json documents in the subbuckets
int i = 0;
// JSONObject buckectinfos1 = new JSONObject();
for (Iterator key = obj1bucket.keys(); key.hasNext();) {
Object jsonkeys;
try {
jsonkeys = obj1bucket.get((String) key.next());
Object jsonvalues = obj1bucket.names().getString(i);
String jsonInString = gson.toJson(jsonkeys);
if (!jsonkeys.getClass().getName().equals("org.json.JSONObject")
&& !jsonkeys.getClass().getName().equals("org.json.JSONArray")) {
} // fin du if
i++;
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} // fin du for
}
}
{
"_shards":"{\"total\":5,\"failed\":0,\"successful\":5}",
"hits":"{\"hits\":[],\"total\":10,\"max_score\":0}",
"took":"7",
"timed_out":"false",
"aggregations":{
"groupe":{
"doc_count_error_upper_bound":"0",
"sum_other_doc_count":"0",
"buckets":[
{
"date":{
"buckets":"{\"key_as_string\":\"2017-05-03T01:00:00.000Z\",\"doc_count\":5,\"value\":{\"value\":6},\"key\":1493773200000}"
},
"doc_count":"4",
"key":"\"b\""
},
{
"date":{
"buckets":"{\"key_as_string\":\"2017-05-03T01:00:00.000Z\",\"doc_count\":5,\"value\":{\"value\":6},\"key\":1493773200000}"
},
"doc_count":"4",
"key":"\"b\""
},
{
"date":{
"buckets":"{\"key_as_string\":\"2017-05-03T01:00:00.000Z\",\"doc_count\":5,\"value\":{\"value\":6},\"key\":1493773200000}"
},
"doc_count":"4",
"key":"\"b\""
},
{
"date":{
"buckets":"{\"key_as_string\":\"2017-05-03T01:00:00.000Z\",\"doc_count\":5,\"value\":{\"value\":6},\"key\":1493773200000}"
},
"doc_count":"4",
"key":"\"b\""
}
]
}
}
}
但我只在JsonArray“ArrayBuckets”中复制了最后一个值:
下面是Json文件:“myjson2.Json”
以下是代码的结果:
package jsonbi.bi;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.simple.parser.JSONParser;
import com.google.gson.Gson;
public class JsonBi9 {
JSONParser parser = new JSONParser();
JSONObject json;
Object obj;
Map.Entry<String, JsonNode> field;
HashMap newmap = new HashMap();
JsonFactory factory = new JsonFactory();
private ObjectMapper mapper = new ObjectMapper();
Gson gson = new Gson();
JSONArray jsonArray = new JSONArray();
JSONObject Alldate = new JSONObject();
JSONObject BucketsObject = new JSONObject();
JSONObject Groupe = new JSONObject();
JSONObject Aggregations = new JSONObject();
JSONObject TheJson = new JSONObject();
JSONArray ArrayBuckets = new JSONArray();
Hashtable ht = new Hashtable();
int j =0;
public JsonBi9() {
try {
// here I am readind the json file
obj = parser.parse(new FileReader("myjson2.json"));
ObjectMapper mapper = new ObjectMapper(factory);
JsonNode rootNode = mapper.readTree(obj.toString());
Iterator<Map.Entry<String, JsonNode>> fieldsIterator = rootNode.getFields();
JSONObject jsonObject = new JSONObject(obj.toString());
// here I am reading the fields in the json fields
while (fieldsIterator.hasNext()) {
field = fieldsIterator.next();
newmap.put(field.getKey(), field.getValue());
if (field.getKey().equals("aggregations")) {
System.out.println("hugo");
JSONObject aggregations = jsonObject.getJSONObject("aggregations");
JSONObject groupe = aggregations.getJSONObject("groupe");
JSONArray buckets = groupe.getJSONArray("buckets");
for (int i11a = 0; i11a < buckets.length(); i11a++) {
JSONObject obj1bucket = buckets.getJSONObject(i11a);
JSONObject date11 = obj1bucket.getJSONObject("date");
JSONArray obj11bucket = date11.getJSONArray("buckets");
List<String> listSubbucket = new ArrayList<String>();
for (int i11 = 0; i11 < obj11bucket.length(); i11++) {
listSubbucket.add(obj11bucket.getJSONObject(i11).toString());
// System.out.println(obj11bucket.getJSONObject(i11).toString());
}
jsonArray = new JSONArray();
for (int i11 = 0; i11 < listSubbucket.size(); i11++) {
JSONObject SubBucket = new JSONObject();
SubBucket.put("buckets", listSubbucket.get(i11));
getdocument(obj1bucket, Alldate);
//******************************here ********************************//
// System.out.println(SubBucket);
Alldate.put("date", SubBucket);
ArrayBuckets.put(Alldate);
//*µµ if you want to print the jsonObject and array
// System.out.println(Alldate);
// System.out.println(ArrayBuckets);*
//*********************************end here ******************************//
} // end first for loop
} //end second for loop
Enumeration e = ht.elements();
while(e.hasMoreElements())
System.out.println(e.nextElement());
getdocument(groupe, Groupe);
Groupe.put("buckets", ArrayBuckets);
Aggregations.put("groupe", Groupe);
// System.out.println(Aggregations);
} // fin du if
else {
// System.out.println("Key: " + field.getKey() + "\tValue:"
// + field.getValue());
TheJson.put(field.getKey(), field.getValue());
} // fin du else
} // fin du while
// I put all the documents
TheJson.put("aggregations", Aggregations);
System.out.println(TheJson);
} catch (Exception e) {
e.printStackTrace();
}
}
public void getdocument(JSONObject obj1bucket, JSONObject Alldate) {
// get the json documents in the subbuckets
int i = 0;
// JSONObject buckectinfos1 = new JSONObject();
for (Iterator key = obj1bucket.keys(); key.hasNext();) {
Object jsonkeys;
try {
jsonkeys = obj1bucket.get((String) key.next());
Object jsonvalues = obj1bucket.names().getString(i);
String jsonInString = gson.toJson(jsonkeys);
if (!jsonkeys.getClass().getName().equals("org.json.JSONObject")
&& !jsonkeys.getClass().getName().equals("org.json.JSONArray")) {
} // fin du if
i++;
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} // fin du for
}
}
{
"_shards":"{\"total\":5,\"failed\":0,\"successful\":5}",
"hits":"{\"hits\":[],\"total\":10,\"max_score\":0}",
"took":"7",
"timed_out":"false",
"aggregations":{
"groupe":{
"doc_count_error_upper_bound":"0",
"sum_other_doc_count":"0",
"buckets":[
{
"date":{
"buckets":"{\"key_as_string\":\"2017-05-03T01:00:00.000Z\",\"doc_count\":5,\"value\":{\"value\":6},\"key\":1493773200000}"
},
"doc_count":"4",
"key":"\"b\""
},
{
"date":{
"buckets":"{\"key_as_string\":\"2017-05-03T01:00:00.000Z\",\"doc_count\":5,\"value\":{\"value\":6},\"key\":1493773200000}"
},
"doc_count":"4",
"key":"\"b\""
},
{
"date":{
"buckets":"{\"key_as_string\":\"2017-05-03T01:00:00.000Z\",\"doc_count\":5,\"value\":{\"value\":6},\"key\":1493773200000}"
},
"doc_count":"4",
"key":"\"b\""
},
{
"date":{
"buckets":"{\"key_as_string\":\"2017-05-03T01:00:00.000Z\",\"doc_count\":5,\"value\":{\"value\":6},\"key\":1493773200000}"
},
"doc_count":"4",
"key":"\"b\""
}
]
}
}
}
在JSONObject中只能有一个名为的条目
for (int i11 = 0; i11 < listSubbucket.size(); i11++) {
JSONObject SubBucket = new JSONObject();
SubBucket.put("buckets", listSubbucket.get(i11));
getdocument(obj1bucket, Alldate);
// System.out.println(SubBucket);
Alldate.put("date", SubBucket);
ArrayBuckets.put(Alldate);
//*µµ if you want to print the jsonObject and array
// System.out.println(Alldate);
// System.out.println(ArrayBuckets);
} // end first for loop
for(int i11=0;i11
执行此操作时,在每个循环中,都会用键“date”覆盖de值。如果你想把你所有的约会都放在AllDate中,那么它必须是一个JSONArray
您还可以替换
Alldate.put(“date”,SubBucket)代码>按Alldate.put(“日期”+i,子桶)代码>不要在类作用域中声明Alldate变量,而是在for循环中声明它。发生此错误是因为只有一个变量JsonObject要放入JsonArray。因此,解决方案是为每个循环创建每个不同的JsonObject变量
for(int i11=0;i11
for (int i11 = 0; i11 < listSubbucket.size(); i11++) {
JSONObject SubBucket = new JSONObject();
SubBucket.put("buckets", listSubbucket.get(i11));
getdocument(obj1bucket, Alldate);
// System.out.println(SubBucket);
Alldate.put("date", SubBucket);
ArrayBuckets.put(Alldate);
//*µµ if you want to print the jsonObject and array
// System.out.println(Alldate);
// System.out.println(ArrayBuckets);
} // end first for loop