![](/img/trans.png)
[英]org.json.JSONException: Value of Feedback type java.lang.String cannot be converted to JSONArray while converting jsonstring to object
[英]Issues Converting String to javax.json.JsonString
我有一堂課,看起來像這樣
private byte[] profilePicture;
private ProfileType profileType;
private String profileHandle;
private String personalEmail;
private Address address;
使用javax.json將其轉換為Json就像這樣:
JsonObject payloadObject = Json.createObjectBuilder()
.add("profilePicture", profilePictureEncoded)
.add("profileType", profileType.ordinal())
.add("profileHandle", profileHandle)
.add("personalEmail", personalEmail)
.add("webAppEmail", webappEmail)
.add("address", address.produceJsonPayload())
.build();
都好!! 除了有時沒有個人資料圖片,所以我想逐步建立這個
JsonObject payloadObject = Json.createObjectBuilder().build();
if(profilePicture != null)
{
final String profilePictureEncoded = ..... //encode the image
payloadObject.put("profilePicture",profilePictureEncoded);
}
問題是您不能,因為我無法將profilePictureEncoded轉換為JsonString
有人有這樣的工作嗎? 還是我需要使用另一個Json庫?
達米安
您可以僅參考JsonObjectBuilder
。
JsonObjectBuilder payloadObjectBuilder = Json.createObjectBuilder();
等到你在構造時擁有一切
if(profilePicture != null)
{
final String profilePictureEncoded = ..... //encode the image
payloadObject.add("profilePicture",profilePictureEncoded);
}
然后build()
JsonObject payloadObject = payloadObjectBuilder.build();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.