将列表数据转换为Json使用javax包类。

huangapple go评论83阅读模式
英文:

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());`

我有一个列表其中的类型是EntityDataEntityData是一个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(&quot;CanonicalName&quot;, 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&lt;EntityData&gt; dataList= new ArrayList&lt;EntityData&gt;(); if I print sout(dataList) then below is my data. it calls toString.

[EntityData [itemname=&quot;Apple Computers&quot;, operation=add], EntityData [itemname=&quot;Infosys&quot;, operation=add], EntityData [itemname=&quot;Wipro&quot;, operation=add], EntityData [itemname=&quot;Google India&quot;, operation=update], EntityData [itemname=&quot;Amazon India&quot;, operation=update], EntityData [itemname=&quot;Capgemini&quot;, operation=add], EntityData [itemname=&quot;Cognizant&quot;, operation=add], EntityData [itemname=&quot;Cognizant&quot;, operation=delete]]

so file contains itemname and operation(add,delete,update). I need to write list data in below format.

    {
&quot;delete&quot;: [
{
&quot;canonicalName&quot;: &quot;TCS&quot;
}
],
&quot;update&quot;: [
{
&quot;canonicalName&quot;: &quot;Google India&quot;
},
{
&quot;canonicalName&quot;: &quot;Amazon India&quot;
}
],
&quot;add&quot;: [
{
&quot;canonicalName&quot;: &quot;Apple Computers&quot;
},
{
&quot;canonicalName&quot;: &quot;Infosys&quot;
},
{
&quot;canonicalName&quot;: &quot;Wipro&quot;
},
{
&quot;canonicalName&quot;: &quot;Capgemini&quot;
},
{
&quot;canonicalName&quot;: &quot;Cognizant&quot;
}
]
}

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 &quot;delete&quot;:
obj.put(&quot;CanonicalName&quot;, data.getItemname() );
delete.add(obj);
break;
case &quot;update&quot;:
obj.put(&quot;CanonicalName&quot;, data.getItemname() );
modify.add(obj);
break;
case &quot;add&quot;:
obj.put(&quot;CanonicalName&quot;, data.getItemname() );
add.add(obj);
break;
default:
break;
}
}
System.out.println(obj);

It is throwing below exception

Exception in thread &quot;main&quot; 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 &quot;delete&quot;:
JsonObject obj1 = JsonObject.EMPTY_JSON_OBJECT; 
obj1.put(&quot;CanonicalName&quot;, data.getItemname() );
delete.add(obj1);
break;
case &quot;update&quot;:
JsonObject obj2 = JsonObject.EMPTY_JSON_OBJECT; 
obj2.put(&quot;CanonicalName&quot;, data.getItemname() );
modify.add(obj2);
break;
case &quot;add&quot;:
JsonObject obj3 = JsonObject.EMPTY_JSON_OBJECT; 
obj3.put(&quot;CanonicalName&quot;, data.getItemname() );
add.add(obj3);
break;
default:
break;
}
}
JsonObject rootObj = JsonObject.EMPTY_JSON_OBJECT; 
rootObj.put(&quot;delete&quot;, delete);
rootObj.put(&quot;update&quot;, modify);
rootObj.put(&quot;add&quot;, add);

huangapple
  • 本文由 发表于 2020年8月4日 22:53:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63249496.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定