Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- public class Simplejson {
- private String body;
- private int numItems;
- private List<String> keys = new ArrayList<String>();
- private List<String> values = new ArrayList<String>();
- public Simplejson() {
- body = "{}";
- }
- private void rebuild() {
- this.body = new String ("{");
- for(int i = 0; i < numItems; i++) {
- String key = "\"" + this.keys.get(i) + "\":";
- String value = "\"" + this.values.get(i) + "\"";
- String line = key + value;
- if(i < numItems - 1) {
- line += ",";
- }
- this.body += line;
- }
- this.body += "}";
- }
- public void add(String newKey, String newValue) {
- if(!this.keys.contains(newKey)) {
- this.keys.add(newKey);
- this.values.add(newValue);
- this.numItems++;
- }
- else {
- this.update(newKey, newValue);
- }
- }
- public void remove(String key) {
- if(this.keys.contains(key)) {
- int index = this.keys.indexOf(key);
- this.keys.remove(index);
- this.values.remove(index);
- this.numItems--;
- }
- }
- public void update(String key, String newValue) {
- if(this.keys.contains(key)) {
- int index = this.keys.indexOf(key);
- this.values.set(index, newValue);
- }
- else {
- this.add(key, newValue);
- }
- }
- public int getLength() {
- return this.numItems;
- }
- public String build() {
- this.rebuild();
- return this.body;
- }
- }
Add Comment
Please, Sign In to add comment