英文:
Converting list data into Json using javax package classes
问题
I can provide you with a translation of the code section you've provided:
我不能使用任何第三方库,所以我在主题行中提到了这一点。
这一行抛出异常。`obj.put("CanonicalName", data.getItemname());`
我有一个列表,其中的类型是EntityData。EntityData是一个POJO,它有2个带有getter和setter以及toString的列。
public class EntityData {
private JsonValue itemname; //如果有帮助解决问题,我可以将数据类型更改为String
private String operation;
}
我的列表是List<EntityData> dataList= new ArrayList<EntityData>();如果我打印sout(dataList),那么下面是我的数据。它调用toString。
[EntityData [itemname="Apple Computers", operation=add], EntityData [itemname="Infosys", operation=add], EntityData [itemname="Wipro", operation=add], EntityData [itemname="Google India", operation=update], EntityData [itemname="Amazon India", operation=update], EntityData [itemname="Capgemini", operation=add], EntityData [itemname="Cognizant", operation=add], EntityData [itemname="Cognizant", operation=delete]]
所以文件包含`itemname`和`operation(add,delete,update)`。我需要以以下格式编写列表数据。
{
"delete": [
{
"canonicalName": "TCS"
}
],
"update": [
{
"canonicalName": "Google India"
},
{
"canonicalName": "Amazon India"
}
],
"add": [
{
"canonicalName": "Apple Computers"
},
{
"canonicalName": "Infosys"
},
{
"canonicalName": "Wipro"
},
{
"canonicalName": "Capgemini"
},
{
"canonicalName": "Cognizant"
}
]
}
我正在编写以下代码,但是遇到错误。
JsonObject obj = JsonObject.EMPTY_JSON_OBJECT;
JsonArray delete= JsonArray.EMPTY_JSON_ARRAY;
JsonArray modify= JsonArray.EMPTY_JSON_ARRAY;
JsonArray add= JsonArray.EMPTY_JSON_ARRAY;
for(EntityData data:dataList)
{
switch(data.getOperation())
{
case "delete":
obj.put("CanonicalName", data.getItemname() );
delete.add(obj);
break;
case "update":
obj.put("CanonicalName", data.getItemname() );
modify.add(obj);
break;
case "add":
obj.put("CanonicalName", data.getItemname() );
add.add(obj);
break;
default:
break;
}
}
System.out.println(obj);
它抛出以下异常
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractMap.put(Unknown Source)
at com.shruti.jdbc.ReadData.main(ReadData.java:86)
我不知道如何处理它。我不能使用任何第三方库。我必须只使用Oracle的Java库。基本上,我不知道如何创建JsonObject对象并将数据插入其中。如果有其他Java库可用,请帮助。
英文:
I can not use any third party library so I have mentioned in subject line.
this line throwing exception. obj.put("CanonicalName", data.getItemname());
I have a list which is of EntityData type
. EntityData
is POJO which has 2 columns with getter setter and toString..
public class EntityData {
private JsonValue itemname; //I can change datatype to String if it helps to reolve issue
private String operation;
}
my list is List<EntityData> dataList= new ArrayList<EntityData>();
if I print sout(dataList) then below is my data. it calls toString.
[EntityData [itemname="Apple Computers", operation=add], EntityData [itemname="Infosys", operation=add], EntityData [itemname="Wipro", operation=add], EntityData [itemname="Google India", operation=update], EntityData [itemname="Amazon India", operation=update], EntityData [itemname="Capgemini", operation=add], EntityData [itemname="Cognizant", operation=add], EntityData [itemname="Cognizant", operation=delete]]
so file contains itemname
and operation(add,delete,update)
. I need to write list data in below format.
{
"delete": [
{
"canonicalName": "TCS"
}
],
"update": [
{
"canonicalName": "Google India"
},
{
"canonicalName": "Amazon India"
}
],
"add": [
{
"canonicalName": "Apple Computers"
},
{
"canonicalName": "Infosys"
},
{
"canonicalName": "Wipro"
},
{
"canonicalName": "Capgemini"
},
{
"canonicalName": "Cognizant"
}
]
}
I was writing below code but i am getting error.
JsonObject obj = JsonObject.EMPTY_JSON_OBJECT;
JsonArray delete= JsonArray.EMPTY_JSON_ARRAY;
JsonArray modify= JsonArray.EMPTY_JSON_ARRAY;
JsonArray add= JsonArray.EMPTY_JSON_ARRAY;
for(EntityData data:dataList)
{
switch(data.getOperation())
{
case "delete":
obj.put("CanonicalName", data.getItemname() );
delete.add(obj);
break;
case "update":
obj.put("CanonicalName", data.getItemname() );
modify.add(obj);
break;
case "add":
obj.put("CanonicalName", data.getItemname() );
add.add(obj);
break;
default:
break;
}
}
System.out.println(obj);
It is throwing below exception
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractMap.put(Unknown Source)
at com.shruti.jdbc.ReadData.main(ReadData.java:86)
. I dont know how to handle it. I can not use any third party library. I have to use java libraries of oracle only. basically I dont know how to create object of JsonObject and insert data into it. If any other java library available pleas help.
答案1
得分: 0
尝试在JsonArray中添加新的JsonObject,并创建一个根对象来添加3种类型的操作数据数组
{
"delete": [
{
"CanonicalName": "data.getItemname()"
}
],
"update": [
{
"CanonicalName": "data.getItemname()"
}
],
"add": [
{
"CanonicalName": "data.getItemname()"
}
]
}
英文:
Try to create new JsonObject when adding in JsonArray and create a root object to add 3 type of operation data array
JsonArray delete= JsonArray.EMPTY_JSON_ARRAY;
JsonArray modify= JsonArray.EMPTY_JSON_ARRAY;
JsonArray add= JsonArray.EMPTY_JSON_ARRAY;
for(EntityData data:dataList)
{
switch(data.getOperation())
{
case "delete":
JsonObject obj1 = JsonObject.EMPTY_JSON_OBJECT;
obj1.put("CanonicalName", data.getItemname() );
delete.add(obj1);
break;
case "update":
JsonObject obj2 = JsonObject.EMPTY_JSON_OBJECT;
obj2.put("CanonicalName", data.getItemname() );
modify.add(obj2);
break;
case "add":
JsonObject obj3 = JsonObject.EMPTY_JSON_OBJECT;
obj3.put("CanonicalName", data.getItemname() );
add.add(obj3);
break;
default:
break;
}
}
JsonObject rootObj = JsonObject.EMPTY_JSON_OBJECT;
rootObj.put("delete", delete);
rootObj.put("update", modify);
rootObj.put("add", add);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论