【问题标题】:Adding json array in a json object in java在java中的json对象中添加json数组
【发布时间】:2018-03-19 18:56:05
【问题描述】:

我正在努力适应 json 对象内的 jsonArray(通过 java 代码)。请帮帮我。

我的输入 JsonObject 是:

{ 
   "products":{  
      "productId":"712161780324",
      "imageURL":"http:example.com/imageResource.jpg",
      "internalItemCode":"N08792 8W"
      }
}

我必须从这个 JSONObject 中读取“imageURL”属性并将其变体附加到同一个 json 对象(图像变体将在 SortedSet 数据结构中)。

样品 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"
      ]
   }
}

样品 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"
           }
      ]
   }
}

我尝试获取示例输出 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);

当我在执行上述逻辑后尝试打印 productDetail JSON 对象时

System.out.println(productDetail.toString());

我观察到以下输出:

{  
   "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"
           }
      ]
     ]
   }
}

如果你注意到,它会像数组数组一样出现(“变体”的额外 []), 请帮助我理解我的逻辑哪里出错了。

另外,请帮我拿出第一个样品。

感谢您的快速响应..

谢谢, 罗希特。

【问题讨论】:

    标签: java json


    【解决方案1】:

    第一个样本可以像这样简单地归档:

        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);
    

    【讨论】:

      【解决方案2】:

      尝试使用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);
      

      来自the docs

      将值附加到键下的数组中。如果 JSONObject 中不存在该键,则将该键放入 JSONObject 中,其值是包含 value 参数的 JSONArray。如果键已经与 JSONArray 关联,则将 value 参数附加到它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多