简体   繁体   English

将JsonObject转换为JsonArray时出错

[英]Error while converting JsonObject to JsonArray

Getting exception with the following code: org.glassfish.json.JsonStringImpl cannot be cast to javax.json.JsonArray 使用以下代码获取异常:org.glassfish.json.JsonStringImpl无法转换为javax.json.JsonArray

JsonObjectParser jop = new JsonObjectParser();
JsonObject jo = Json.createObjectBuilder().add("yAxisValue","Yvalue")
            .add("xAxisValue","Xvalue").build();
jo.getJsonArray("xAxisValue");

Note: using javax.json java API 注意:使用javax.json java API

The error is the following 错误如下

.add("xAxisValue","Xvalue")

the statement above is not to create a json array , the json string you created is the following 上面的声明不是要创建一个json array ,您创建the json string如下

{ 
  "yAxisValue":"Yvalue",
  "xAxisValue":"Xvalue"
}

not

{ 
  "yAxisValue":"Yvalue",
  "xAxisValue":["Xvalue"]
}

you should use the function add("key",Json.createArrayBuilder(...)) 您应该使用功能add("key",Json.createArrayBuilder(...))

hope that helped 希望能有所帮助

For creating JSON Array by javax.json use following method. 要通过javax.json创建JSON数组,请使用以下方法。

JsonArray value = Json.createArrayBuilder()
     .add(Json.createObjectBuilder()
         .add("type", "home")
         .add("number", "212 555-1234"))
     .add(Json.createObjectBuilder()
         .add("type", "fax")
         .add("number", "646 555-4567"))
     .build();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM