Java
Chilkat2 Java
JSON: Renaming and Deleting Members
See more JSON Examples
Demonstrates renaming and deleting members. This example uses the following JSON document:
{
"apple": "red",
"lime": "green",
"banana": "yellow",
"broccoli": "green",
"strawberry": "red"
}
Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.JsonObject;
public class ChilkatExample {
public static void main(String[] args) {
JsonObject json = new JsonObject();
try {
json.load("{\"apple\": \"red\",\"lime\": \"green\",\"banana\": \"yellow\",\"broccoli\": \"green\",\"strawberry\": \"red\"}");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Rename "lime" to "lemon".
json.rename("lime", "lemon");
// Change the color to yellow:
json.setStringOf("lemon", "yellow");
// Rename by index. Banana is at index 2 (apple is at index 0)
json.renameAt(2, "bartlett_pear");
// Delete broccoli by name
json.delete("broccoli");
// Delete apple by index. Apple is at index 0.
json.deleteAt(0);
json.setEmitCompact(false);
System.out.println(json.emit());
}
}