[英]Adding json array in a json object in java
I am struggling to fit in a jsonArray inside a json object (through java code).. please help me out. 我正在努力适应json对象内的jsonArray(通过java代码)..请帮助我。
My Input JsonObject is : 我的输入JsonObject是:
{
"products":{
"productId":"712161780324",
"imageURL":"http:example.com/imageResource.jpg",
"internalItemCode":"N08792 8W"
}
}
I will have to read "imageURL" property from this JSONObject and append its variants to the same json object (image variants will be in SortedSet data structure). 我将必须从此JSONObject读取“ imageURL”属性,并将其变体附加到同一json对象(图像变体将在SortedSet数据结构中)。
Sample O/P 1 : 样品O / P 1:
{
"products":{
"productId":"712161780324",
"imageURL":"http:example.com/imageResource.jpg",
"internalItemCode":"N08792 8W",
"variants":[
"http:example.com/imageResource_variant1.jpg",
"http:example.com/imageResource_variant2.jpg"
]
}
}
Sample O/P 2 : O / P 2样本:
{
"products":{
"productId":"712161780324",
"imageURL":"http:example.com/imageResource.jpg",
"internalItemCode":"N08792 8W",
"variants":[
{
"url" : "http:example.com/imageResource_variant1.jpg"
},
{
"url" : "http:example.com/imageResource_variant2.jpg"
}
]
}
}
The logic i tried to get sample output 2 is some what like below, 我尝试获取示例输出2的逻辑如下所示,
// productDetail is the give input JSONObject
JSONObject product = productDetail.optJSONObject("products");
SortedSet<String> imageUrls = new TreeSet<>();
imageUrls.add("http:example.com/imageResource_variant1.jpg");
imageUrls.add("http:example.com/imageResource_variant2.jpg");
Iterator<String> itr = imageUrls.iterator();
JSONArray imageUrlsArray = new JSONArray();
while (itr.hasNext()) {
JSONObject imageUrlObj = new JSONObject();
imageUrlObj.put("url", itr.next());
imageUrlsArray.put(imageUrlObj);
}
product.append("variants", imageUrlsArray);
When i tried to print the productDetail JSON object after executing above logic 当我尝试执行上述逻辑后尝试打印productDetail JSON对象时
System.out.println(productDetail.toString()); System.out.println(productDetail.toString());
I observed the following output : 我观察到以下输出:
{
"products":{
"productId":"712161780324",
"imageURL":"http:example.com/imageResource.jpg",
"internalItemCode":"N08792 8W",
"variants":[
[
{
"url" : "http:example.com/imageResource_variant1.jpg"
},
{
"url" : "http:example.com/imageResource_variant2.jpg"
}
]
]
}
}
If you notice, It's coming up like Array of arrays (extra [ ] for "variants"), Please help me in understanding Where my logic is going wrong. 如果您注意到,它的出现就像是数组数组(“ [variant]的额外[]”)一样,请帮助我了解我的逻辑哪里出了问题。
And also, Please help me getting the First sample out put. 另外,请帮助我获得第一批样品。
Appreciate quick response.. 赞赏迅速的反应。
Thanks, Rohit. 谢谢,罗希特。
First sample can be archivable as simple as this: 第一个样本可以像这样简单地归档:
JSONObject product = productDetail.optJSONObject("products");
JSONArray imageUrlsArray = new JSONArray();
imageUrlsArray.put(0, "http:example.com/imageResource_variant1.jpg");
imageUrlsArray.put(1, "http:example.com/imageResource_variant2.jpg");
product.append("variants", imageUrlsArray);
Try using put
instead of append
: 尝试使用
put
而不是append
:
JSONObject product = productDetail.optJSONObject("products");
SortedSet<String> imageUrls = new TreeSet<>();
imageUrls.add("http:example.com/imageResource_variant1.jpg");
imageUrls.add("http:example.com/imageResource_variant2.jpg");
Iterator<String> itr = imageUrls.iterator();
JSONArray imageUrlsArray = new JSONArray();
while (itr.hasNext()) {
JSONObject imageUrlObj = new JSONObject();
imageUrlObj.put("url", itr.next());
imageUrlsArray.put(imageUrlObj);
}
-product.append("variants", imageUrlsArray);
+product.put("variants", imageUrlsArray);
Append values to the array under a key.
将值附加到键下的数组。 If the key does not exist in the JSONObject, then the key is put in the JSONObject with its value being a JSONArray containing the value parameter.
如果该密钥在JSONObject中不存在,则将该密钥放入JSONObject中,其值是一个包含value参数的JSONArray。 If the key was already associated with a JSONArray, then the value parameter is appended to it.
如果键已经与JSONArray关联,则将value参数附加到该键。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.