[英]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.