简体   繁体   English

从XAgent创建Json

[英]Creating Json from XAgent

I'm trying to create some Json from an XPages "XAgent" using Java. 我正在尝试使用Java从XPages“ XAgent”创建一些Json。 There's a particular format I'm trying to it and it uses an Integer as a key and I keep getting some errors that I don't understand. 我正在尝试一种特殊的格式,它使用Integer作为键,并且不断收到一些我不理解的错误。

Here's an example error: Caused by: java.lang.ClassCastException: java.lang.Integer incompatible with java.lang.String at com.ibm.commons.util.io.json.JsonGenerator$Generator.outObject(JsonGenerator.java:202) at com.ibm.commons.util.io.json.JsonGenerator$Generator.outLiteral(JsonGenerator.java:163) at com.ibm.commons.util.io.json.JsonGenerator$Generator.outLiteral(JsonGenerator.java:142) at com.ibm.commons.util.io.json.JsonGenerator$Generator.toJson(JsonGenerator.java:138) at com.ibm.commons.util.io.json.JsonGenerator.toJson(JsonGenerator.java:64) at com.ibm.commons.util.io.json.JsonGenerator.toJson(JsonGenerator.java:49) 这是一个示例错误:由以下原因引起: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)

I'm trying to create JSon output like this: 我正在尝试像这样创建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
     }

]      

Not it is multiple "objects" and the last one seems to be a combination of Integer and String for the key value. 不是它是多个“对象”,最后一个似乎是键值的整数和字符串的组合。

Here is some code I tried and got working kinda of: 这是我尝试过并可以正常工作的一些代码:

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;

    }

this code produces: 此代码产生:

{
    "1": "Zero",
    "2": "One",
    "3": "Two"
}

Note the quotes for the key value. 请注意关键值的引号。 I assume that's going to be a problem. 我认为这将是一个问题。 I was enable to get Integers in for the key. 我能够获取Integers作为密钥。 And I'm not sure how to mix Integers with String as seen in the 3rd object example. 而且我不确定如何在第3个对象示例中看到如何将Integers与String混合使用。

Any advice would be appreciated. 任何意见,将不胜感激。 Thanks! 谢谢!

It cannot be used an integer in this way: {0: {...}} The properties are supposed to be strings: {"0": {...}} 不能以这种方式使用整数:{0:{...}}属性应该是字符串:{“ 0”:{...}}

Maybe you need an array instead: 也许您需要一个数组来代替:

{ // 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 } }

Regards, Txemanu 问候,Txemanu

Use Gson. 使用Gson。 Saves you ton of headaches. 节省您很多头痛。 Must be in a plug-in for security to work. 必须在插件中才能确保安全性。 Make a result class with collections and maps and whatever. 使用集合和地图以及其他内容创建结果类。 Then have 2 lines: 然后有2行:

   Gson g = new Gson();
   g.toJson(this);

There is a builder and a few annotations for options like pretty print or naming elements different from the variable names. 有一个生成器和一些注释,用于一些漂亮的打印或命名与变量名称不同的元素。

Pecked on glass at high altitude. 啄在高空的玻璃上。 Will contain typos. 将包含错别字。

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

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