[英]Creating Json from XAgent
我正在嘗試使用Java從XPages“ XAgent”創建一些Json。 我正在嘗試一種特殊的格式,它使用Integer作為鍵,並且不斷收到一些我不理解的錯誤。
這是一個示例錯誤:由以下原因引起:java.lang.ClassCastException:java.lang.Integer與com.ibm.commons.util.io.json.JsonGenerator $ Generator.outObject(JsonGenerator.java:202上的java.lang.String不兼容)在com.ibm.commons.util.io.json.JsonGenerator $ Generator.outLiteral(JsonGenerator.java:163)在com.ibm.commons.util.io.json.JsonGenerator $ Generator.outLiteral(JsonGenerator.java:142 )在com.ibm.commons.util.io.json.JsonGenerator $ Generator.toJson(JsonGenerator.java:138)在com.ibm.commons.util.io.json.JsonGenerator.toJson(JsonGenerator.java:64)在com.ibm.commons.util.io.json.JsonGenerator.toJson(JsonGenerator.java:49)
我正在嘗試像這樣創建JSon輸出:
[
{
// minimum
0:{src:'item_one_format_one.ext', type: 'video/...'}
},
{
// one content, multiple formats
0:{src:'item_two_format_one.ext', type: 'video/...'},
1:{src:'item_two_format_two.ext', type: 'video/...'}
},
{
// one content, multiple formats, item specific config
0:{src:'item_three_format_one.ext', type: 'video/...'},
1:{src:'item_three_format_two.ext', type: 'video/...'},
3:{src:'item_three_format_three.ext', type: 'video/...'},
4:{src:'item_three_format_four.ext', type: 'video/...'},
config: {
option1: value1,
option2: value2
}
]
不是它是多個“對象”,最后一個似乎是鍵值的整數和字符串的組合。
這是我嘗試過並可以正常工作的一些代碼:
public String testList() throws JsonException, IOException {
Integer count = 0;
Map<String, Object> containerMap = new TreeMap<String, Object>();
System.out.println("A");
TreeMap<String, String> stringMap = new TreeMap<String, String>();
System.out.println("B");
stringMap.put("One", "Value1");
stringMap.put("Two", "Value2");
System.out.println("C");
containerMap.put("1", "One");
count++;
containerMap.put("2", "Two");
count++;
containerMap.put("3", "Three");
System.out.println("D");
String json = JsonGenerator.toJson(new JsonJavaFactory(), containerMap);
System.out.println("E");
return json;
}
此代碼產生:
{
"1": "Zero",
"2": "One",
"3": "Two"
}
請注意關鍵值的引號。 我認為這將是一個問題。 我能夠獲取Integers作為密鑰。 而且我不確定如何在第3個對象示例中看到如何將Integers與String混合使用。
任何意見,將不勝感激。 謝謝!
不能以這種方式使用整數:{0:{...}}屬性應該是字符串:{“ 0”:{...}}
也許您需要一個數組來代替:
{ // one content, multiple formats, item specific config videoList: [ {src:'item_three_format_one.ext', type: 'video/...'}, {src:'item_three_format_two.ext', type: 'video/...'}, {src:'item_three_format_three.ext', type: 'video/...'}, {src:'item_three_format_four.ext', type: 'video/...'} ], vidoConfig: { option1: value1, option2: value2 } }
問候,Txemanu
使用Gson。 節省您很多頭痛。 必須在插件中才能確保安全性。 使用集合和地圖以及其他內容創建結果類。 然后有2行:
Gson g = new Gson();
g.toJson(this);
有一個生成器和一些注釋,用於一些漂亮的打印或命名與變量名稱不同的元素。
啄在高空的玻璃上。 將包含錯別字。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.