将Notes字段转换为JsonJavaArray

时间:2018-11-17 08:08:56

标签: java json xpages

在我的XPages应用程序中,我使用com.ibm.commons.util.io.json.JsonJavaObject类来获取一系列的联系方式(电话列表)。

我想绑定到重复控件的对象,该控件将显示联系人的所有联系人详细信息。

我想将JsonJavaArray的值保存到Notes字段中,当我通过XPage显示文档时,我想再次读取它,因此必须进行转换。

这是在字段中保存json的方式:

{
    "locations": [
        {
            "lat": "23.053",
            "long": "72.629",
            "location": "ABC",
            "address": "DEF",
            "city": "Ahmedabad",
            "state": "Gujrat",
            "phonenumber": "1234567"
        },
        {
            "lat": "23.053",
            "long": "72.629",
            "location": "ABC",
            "address": "DEF",
            "city": "Ahmedabad",
            "state": "Gujrat",
            "phonenumber": "1234567"
        },
        {
            "lat": "23.053",
            "long": "72.629",
            "location": "ABC",
            "address": "DEF",
            "city": "Ahmedabad",
            "state": "Gujrat",
            "phonenumber": "1234567"
        },
        {
            "lat": "23.053",
            "long": "72.629",
            "location": "ABC",
            "address": "DEF",
            "city": "Ahmedabad",
            "state": "Gujrat",
            "phonenumber": "1234567"
        },
        {
            "lat": "23.053",
            "long": "72.629",
            "location": "ABC",
            "address": "DEF",
            "city": "Ahmedabad",
            "state": "Gujrat",
            "phonenumber": "1234567"
        }
    ]
}

这是我读取字段并将其转换为jsonobject并读取数组的方式:

String codeString = doc.getItemValueString(fieldName);
JsonJavaObject objJson = new JsonJavaObject(codeString);                
JsonJavaArray arrJson = objJson.getAsArray("locations");

我得到的错误如下:

  

com.ibm.commons.util.io.json.JsonJavaObject中的错误,第56行:   java.lang.ArrayIndexOutOfBoundsException:数组索引超出范围:1

有人可以向我解释我在做什么错吗?

1 个答案:

答案 0 :(得分:1)

您应该使用JsonParser从String对象创建JSON对象,例如

if (day == 1){System.out.print("first");}

if (month == 1){System.out.println("Your birthday is " + day + " January");}

更多示例可以在这里找到:

https://www.programcreek.com/java-api-examples/?api=com.ibm.commons.util.io.json.JsonParser