Ich habe heute viel für dieses Thema gegoogelt. Aber ich kann es nicht finden. Wie kann ich einem JSONObject einen JSONArray hinzufügen?
Jedes Mal, wenn ich das tue, erhalte ich diesen Fehler: Stackoverflow
JSONObject fillBadkamerFormaatFromContentlet(Structure structure, String formaat) {
JSONObject jsonObject = new JSONObject();
JSONArray arr = new JSONArray();
BadkamerFormaat badkamerFormaat = new BadkamerFormaat();
BadkamerTegel badkamerTegel;
List<Contentlet> contentlets = getContentletsByStructure(structure);
badkamerFormaat.formaat = formaat;
badkamerFormaat.tegels = new ArrayList<BadkamerTegel>();
try {
jsonObject.put("formaat", formaat);
} catch (JSONException e1) {
throw new RuntimeException(e1);
}
for(Contentlet contentlet : contentlets) {
badkamerTegel = new BadkamerTegel();
badkamerTegel.naam = contentlet.getStringProperty(ParameterNames.toolBetegelVeldNaam);
try {
badkamerTegel.afbeeldingTegel = contentlet.getBinary(ParameterNames.toolBetegelVeldTegelAfbeelding).getPath();
badkamerTegel.afbeeldingBadkamer = contentlet.getBinary(ParameterNames.toolBetegelVeldBadkamerAfbeelding).getCanonicalPath();
arr.put(badkamerTegel.toJSON());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
try {
jsonObject.put("aoColumnDefs",arr);
} catch (JSONException e) {
throw new RuntimeException(e);
}
return jsonObject;
}
Ich erhalte diesen Fehler:
Java.lang.StackOverflowError
at com.dotmarketing.util.json.JSONArray.<init>(JSONArray.Java:248)
at com.dotmarketing.util.json.JSONObject.put(JSONObject.Java:953)
at com.dotmarketing.util.json.JSONObject.put(JSONObject.Java:953)
at com.dotmarketing.util.json.JSONObject.put(JSONObject.Java:953)
at com.dotmarketing.util.json.JSONObject.put(JSONObject.Java:953)
at com.dotmarketing.util.json.JSONObject.put(JSONObject.Java:953)
at com.dotmarketing.util.json.JSONObject.put(JSONObject.Java:953)
at com.dotmarketing.util.json.JSONObject.put(JSONObject.Java:953)
at com.dotmarketing.util.json.JSONObject.put(JSONObject.Java:953)
at com.dotmarketing.util.json.JSONObject.put(JSONObject.Java:953)
at com.dotmarketing.util.json.JSONObject.put(JSONObject.Java:953)
at com.dotmarketing.util.json.JSONObject.put(JSONObject.Java:953)
at com.dotmarketing.util.json.JSONObject.put(JSONObject.Java:953)
at com.dotmarketing.util.json.JSONObject.put(JSONObject.Java:953)
at com.dotmarketing.util.json.JSONObject.put(JSONObject.Java:953)
at com.dotmarketing.util.json.JSONObject.put(JSONObject.Java:953)
at com.dotmarketing.util.json.JSONObject.put(JSONObject.Java:953)
at com.dotmarketing.util.json.JSONObject.put(JSONObject.Java:953)
at com.dotmarketing.util.json.JSONObject.put(JSONObject.Java:953)
at com.dotmarketing.util.json.JSONObject.put(JSONObject.Java:953)
at com.dotmarketing.util.json.JSONObject.put(JSONObject.Java:953)
at com.dotmarketing.util.json.JSONObject.put(JSONObject.Java:953)
at com.dotmarketing.util.json.JSONObject.put(JSONObject.Java:953)
at com.dotmarketing.util.json.JSONObject.put(JSONObject.Java:953)
Die JSON, die ich will: Nur der letzte JsonArray läuft schief:
{
"wand": [
{
formaat: 'vierkant15x15'
tegels: [
{naam: '', imgThumb: '/bla/bla.png', largeImg: '/bla/bla2.png'}
,{naam: '', imgThumb: '/bla/bla.png', largeImg: '/bla/bla2.png'}
]
}
,
{
formaat: 'vierkant17x15'
tegels: [
{naam: '', imgThumb: '/bla/bla.png', largeImg: '/bla/bla2.png'}
,{naam: '', imgThumb: '/bla/bla.png', largeImg: '/bla/bla2.png'}
]
}
]
, "vloer": [ { Format: 'vierkant10x15' Tegels: [ {naam: '', imgThumb: '/bla/bla.png', largeImg: '/bla/bla2.png'} , {naam: '', imgThumb: '/bla/bla.png', largeImg: '/bla/bla2.png'} ] } .
{
formaat: 'vierkant45x15'
tegels: [
{naam: '', imgThumb: '/bla/bla.png', largeImg: '/bla/bla2.png'}
,{naam: '', imgThumb: '/bla/bla.png', largeImg: '/bla/bla2.png'}
]
}
]
}
Ich denke, es ist ein Problem (aka Fehler) mit der API , die Sie verwenden. JSONArray
implementiert Collection
(die json.org-Implementierung, von der diese API abgeleitet ist, hat nicht eine JSONArray-Implementierungsauflistung). Und JSONObject
hat eine überladene put()
-Methode, die eine Collection nimmt und in eine JSONArray
umschließt (wodurch das Problem verursacht wird). Ich denke, Sie müssen die Verwendung der anderen JSONObject.put()
-Methode erzwingen:
jsonObject.put("aoColumnDefs",(Object)arr);
Sie sollten einen Fehler beim Hersteller einreichen, ziemlich sicher, dass ihre JSONObject.put(String,Collection)
-Methode fehlerhaft ist.
hier ist einfacher Code
List <String> list = new ArrayList <String>();
list.add("a");
list.add("b");
JSONArray array = new JSONArray();
for (int i = 0; i < list.size(); i++) {
array.put(list.get(i));
}
JSONObject obj = new JSONObject();
try {
obj.put("result", array);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pw.write(obj.toString());
Deine Liste:
List<MyCustomObject> myCustomObjectList;
Dein JSONArray:
// Don't need to loop through it. JSONArray constructor do it for you.
new JSONArray(myCustomObjectList)
Deine Antwort:
return new JSONObject().put("yourCustomKey", new JSONArray(myCustomObjectList));
Ihre Post/Put-HTTP-Body-Anfrage würde folgendermaßen aussehen:
{
"yourCustomKey: [
{
"myCustomObjectProperty": 1
},
{
"myCustomObjectProperty": 2
}
]
}
Ich fange an, mehr über mich selbst zu erfahren, da ich in der Android-Entwicklung noch sehr neu bin und dieses Video sehr hilfreich war.
https://www.youtube.com/watch?v=qcotbMLjlA4
Es wird ausdrücklich darauf hingewiesen, JSONArray um 19:30 Uhr im Video zu JSONObject zu bringen.
Code aus dem Video für JSONArray zu JSONObject:
JSONArray queryArray = quoteJSONObject.names();
ArrayList<String> list = new ArrayList<String>();
for(int i = 0; i < queryArray.length(); i++){
list.add(queryArray.getString(i));
}
for(String item : list){
Log.v("JSON ARRAY ITEMS ", item);
}
Sie können dies einfach mit der Json Simple-Bibliothek tun. Hier ist der Grad
compile 'com.googlecode.json-simple:json-simple:1.1'
Hier ist Beispielcode:
org.json.simple.JSONObject jsonObject=new org.json.simple.JSONObject();
jsonObject.put("Object","String Object");
ArrayList<String> list = new ArrayList<String>();
list.add("john");
list.add("mat");
list.add("jason");
list.add("matthew");
jsonObject.put("List",list);
Das ist es. :)