I have an existing jsonobject from the javax.json.JsonObject class.
我有一个来自javax.json的现有jsonobject。JsonObject类。
I can't for the life of me figure out how I can modify the existing values in it. Ideally I'd like to do something like this:
我不知道如何修改它的现有值。理想情况下,我想做这样的事情:
if(object.getString("ObjectUUID").length()==0){
object.put("ObjectUUID", UUID.randomUUID().toString());
}
According to the API you aren't allowed to modify that map.
根据API,您不允许修改映射。
http://docs.oracle.com/javaee/7/api/javax/json/JsonObject.html
http://docs.oracle.com/javaee/7/api/javax/json/JsonObject.html
This map object provides read-only access to the JSON object data, and attempts to modify the map, whether direct or via its collection views, result in an UnsupportedOperationException.
这个map对象提供对JSON对象数据的只读访问,并尝试修改映射,无论是直接的还是通过它的集合视图,都会导致UnsupportedOperationException。
Currently I'm getting around the problem with a quick hack but there must be a better solution than this:
目前我正在用一种快速的方法来解决这个问题,但是肯定有比这个更好的解决方案:
if(object.getString("ObjectUUID").length()==0){
JsonObjectBuilder job = Json.createObjectBuilder();
job.add("ObjectUUID", UUID.randomUUID().toString());
for(String key : object.keySet()){
if(!key.equals("ObjectUUID")){
job.add(key, object.get(key));
}
}
object = job.build();
}
So the question how do you modify an existing jsonobject?
问题是如何修改现有的jsonobject?
2 个解决方案
#1
0
GSON指南;
obj.addProperty("Id", "001");
System.out.println("Before: " + obj.get("Id")); // 001
obj.addProperty("Id", "002");
System.out.println("After: " + obj.get("Id")); // 002
#2
1
I think there is no other way to modify a javax.json.JsonObject.
我认为没有其他方法可以修改javax.json.JsonObject。
You should consider using jackson-databind.
您应该考虑使用jackson-databind。
#1
0
GSON指南;
obj.addProperty("Id", "001");
System.out.println("Before: " + obj.get("Id")); // 001
obj.addProperty("Id", "002");
System.out.println("After: " + obj.get("Id")); // 002
#2
1
I think there is no other way to modify a javax.json.JsonObject.
我认为没有其他方法可以修改javax.json.JsonObject。
You should consider using jackson-databind.
您应该考虑使用jackson-databind。