将 JsonArray 添加到 JsonObject [英] Add JsonArray to JsonObject
问题描述
我今天在谷歌上搜索了很多这个主题.但是我找不到它,如何将 JSONArray 添加到 JSONObject?
I googled a lot today for this subject. But I can't find it, How can I add a JSONArray to a JSONObject?
因为每次我这样做我都会收到这个错误:Stackoverflow
Because everytime I do this I get this error: 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;
}
我收到此错误:
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)
我想要的 JSON:只有最后一个 JsonArray 出错了:
The JSON I want: Only the last JsonArray is going wrong:
{
"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":[{格式:'virkant10x15'泰格尔:[{naam: '', imgThumb: '/bla/bla.png', largeImg: '/bla/bla2.png'},{naam: '', imgThumb: '/bla/bla.png', largeImg: '/bla/bla2.png'}]},
, "vloer": [ { formaat: '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'}
]
}
]
}
推荐答案
我认为 API.JSONArray
实现了 Collection
(此 API 派生自的 json.org 实现没有让 JSONArray 实现 Collection).并且 JSONObject
有一个重载的 put()
方法,它接受一个集合并将它包装在一个 JSONArray
中(从而导致问题).我认为您需要强制使用其他 JSONObject.put()
方法:
I think it is a problem(aka. bug) with the API you are using. JSONArray
implements Collection
(the json.org implementation from which this API is derived does not have JSONArray implement Collection). And JSONObject
has an overloaded put()
method which takes a Collection and wraps it in a JSONArray
(thus causing the problem). I think you need to force the other JSONObject.put()
method to be used:
jsonObject.put("aoColumnDefs",(Object)arr);
您应该向供应商提交错误,很确定他们的 JSONObject.put(String,Collection)
方法已损坏.
You should file a bug with the vendor, pretty sure their JSONObject.put(String,Collection)
method is broken.
这篇关于将 JsonArray 添加到 JsonObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!