简体   繁体   中英

Creating Json from XAgent

I'm trying to create some Json from an XPages "XAgent" using Java. 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.

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)

I'm trying to create JSon output like this:

[
  {
     // 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. And I'm not sure how to mix Integers with String as seen in the 3rd object example.

Any advice would be appreciated. Thanks!

It cannot be used an integer in this way: {0: {...}} The properties are supposed to be strings: {"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

Use 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:

   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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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