关于java:从XAgent创建Json | 珊瑚贝

Creating Json from XAgent


我正在尝试使用 Java 从 XPages”XAgent”创建一些 Json。我正在尝试一种特殊的格式,它使用整数作为键,我不断收到一些我不理解的错误。

这是一个错误示例:
引起:java.lang.ClassCastException:java.lang.Integer 与 java.lang.String 不兼容
在 com.ibm.commons.util.io.json.JsonGenerator$Generator.outObject(JsonGenerator.java:202)
在 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 输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[
  {
     // 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
     }

]

不是它是多个”对象”,最后一个似乎是键值的整数和字符串的组合。

这是我尝试过的一些代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
2
3
4
5
{
   “1”:“Zero”,
   “2”:“One”,
   “3”:“Two”
}

注意键值的引号。我认为这将是一个问题。我能够获得整数作为密钥。而且我不确定如何将整数与字符串混合,如第三个对象示例中所示。

任何建议将不胜感激。谢谢!

  • Afaik Json 键必须是字符串
  • 我第二点,键必须是字符串。第二个 JSON 是正确的,JSONLint 是你的朋友:jsonlint.com
  • 只有字符串?嗯,我不知道。我只是真正尝试过尝试为 Projekktor 项目打这种格式:projekktor.com/docs/playlists,它显示了前导整数。也许它会在数字周围的双引号中正常工作。我会试试看。谢谢


不能以这种方式使用整数:
{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

  • 那是Java还是SSJS?这看起来更像是我之前做过的 SSJS,然后我做了 “toJson()” 来返回字符串。
  • 我只是在谈论json。不管它是用 Java 还是 SSJS 生成的。


使用 Gson。为您节省大量头痛。必须在插件中才能使安全工作。用集合和地图等创建一个结果类。然后有 2 行:

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

有一个构建器和一些注释用于选项,例如漂亮的打印或与变量名称不同的命名元素。

在高空啄玻璃。将包含错别字。

  • OpenNTF上有GSon插件吗?那不是我自己知道该怎么做的事情。
  • 创建一个插件。添加 Gson 罐子。定义要重新导出的类
  • 那么你的服务器上安装了 Verse 吗?插件包含 Googlea€?s Gson 和 Guava 库


来源:https://www.codenong.com/23975133/

微信公众号
手机浏览(小程序)
0
分享到:
没有账号? 忘记密码?