[Solved] Adding new entry to JSON file

edited February 2015 in Questions about Code

Hi, I am trying to add new content to JSON file. What happens is that when I try to save the new entry it does save it in the file but replace the old content and a null before the JSONObject.

Basically when you type you can see the text on the screen which is suppose to be saved in the JSON file along with the old data so that when I load the JSON file next time it show me all the entry.

Here is the code

DateStamp D;
String saveText = " ";

JSONArray  jsonLoad;
JSONArray jsonSave;
JSONObject tempText;  

int id;
String Date, Message;
void setup() {
  size(400, 400, P3D); 
  D = new DateStamp();
  jsonLoad = loadJSONArray("data/data.json");
  jsonSave =  new JSONArray();

  tempText = new JSONObject();
}

void draw() {
  background(-1);
  saveButton();
  fill(0);
  //----load Jason Array -----
  for (int i = 0; i < jsonLoad.size (); i++) {
    JSONObject tempTask = jsonLoad.getJSONObject(i); 
    id = tempTask.getInt("id");
    Date = tempTask.getString("Date");
    Message = tempTask.getString("Message");
    text(Date + "\n\n" + Message, 10, 10);
  }
  //-----Save json array ----------------
  text(saveText, 30, 30);
  if (save) {
    tempText.setInt("id", jsonLoad.size ()+1);
    tempText.setString("Date", D.createStamp());
    tempText.setString("Message", saveText);
    jsonSave.setJSONObject(jsonLoad.size ()+1, tempText);
    saveJSONArray(jsonSave, "data/data.json");
    save=false;
  }
}

void keyPressed() {
  if (key == BACKSPACE) {
    if (saveText.length()>0) {
      saveText = saveText.substring(0, saveText.length()-1);
    }
  } else if (key == CODED) {
    if (keyCode == SHIFT) {
    }
  } else {
    saveText+= key;
  }
}

boolean save=false;
void mousePressed() {
  if (dist(mouseX, mouseY, 0, height)<50) {
    save=true;
    println("SAVED");
  }
}

void mouseWheel(MouseEvent event) {
  float e = event.getCount();
  println(e);
}


void saveButton() {
  fill(#DE1036);
  noStroke();
  rect(0, height-30, 50, 30);
  fill(-1);
  text("SAVE", 10, height-10);
}




class DateStamp {
  int d = day();     
  int mth = month(); 
  int y = year();
  int s = second();  // Values from 0 - 59
  int m = minute();  // Values from 0 - 59
  int h = hour();

  String[] month = {
    "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  };

  String createStamp() { 
    return d +" " + month[mth]+" "+y +"    "+ ((h>12)?h-12:h) +":"+ nf(m, 2);
  }
}

JSON

// before
[
    {
    "id": 2,
    "Message": " MURISKAGI KUAGA ",
    "Date": "12 Mar 2015    11:12"
  }
]


//after ///how to add a new entry instead of null
[ 
   null,
    {
    "id": 2,
    "Message": " LOLU KUMPA SON TUDO",
    "Date": "12 Mar 2015    11:12"
  }
]

Answers

  • edited February 2015

    I have solved this problem using XML but I would like know to how to do the same in JSON. I am sharing my code here so that whoever find it useful, can use it.

    I will try again for JSON and if get successful I would post the code here.

    XML xml;
    XML[] children;
    DateStamp D = new DateStamp();
    String saveText = " ";
    
    void setup() {
      size(300, 600);
      updateXML();
    }
    void updateXML() {
      xml = loadXML("TaskManager.xml");
      children = xml.getChildren("Task");
    }
    void draw() {
      background(-1);
      saveButton();
      updateXML();
      for (int i = 0; i < children.length; i++) {
        int id = children[i].getInt("id");
        String date = children[i].getString("date");
        String thetask = children[i].getContent();
        Message M = new Message(id, date, thetask);
        fill((i%2==0)?#FF8181:#F70000);
        rect(0, i*35, width, 40);
        fill(0);
        pushMatrix();
        translate(10, i*37);
        M.show();
        popMatrix();
      }
    
      text(saveText, 10, 80);
    
      if (save) {
        XML newChild = xml.addChild("Task");
        newChild.setInt("id", children.length );
        newChild.setString("date", D.createStamp());
        newChild.setContent(saveText);
        saveXML(xml, "TaskManager.xml");
        save =false;
        saveText = " ";
      }
    }
    
    //-----------------------------------------
    class DateStamp {
      int d = day();     
      int mth = month(); 
      int y = year();
      int s = second();  // Values from 0 - 59
      int m = minute();  // Values from 0 - 59
      int h = hour();
    
      String[] month = {
        "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
      };
    
      String createStamp() { 
        return d +" " + month[mth]+" "+y +"    "+ ((h>12)?h-12:h) +":"+ nf(m, 2);
      }
    }
    //------------------------------------------
    void keyPressed() {
      if (key == BACKSPACE) {
        if (saveText.length()>0) {
          saveText = saveText.substring(0, saveText.length()-1);
        }
      } else if (key == CODED) {
        if (keyCode == SHIFT) {
        }
      } else {
        saveText+= key;
      }
    }
    
    boolean save=false;
    void mousePressed() {
      if (dist(mouseX, mouseY, 0, height)<50) {
        save=true;
        println("SAVED");
      }
    }
    void saveButton() {
      fill(#DE1036);
      noStroke();
      rect(0, height-30, 50, 30);
      fill(-1);
      text("SAVE", 10, height-10);
    }
    //------------------------------------
    class Message {
      int id;
      String date = " ";
      String message = " ";
      Message(int id, String date, String message) {
        this.id = id;
        this.date = date;
        this.message = message;
      }
      void show() {
        textSize(12);
        text(id+" "+ date, 0, 10);
        textSize(16);
        text(message, 0, 24);
      }
    }
    

    :) :) :)

  • In the first code, you don't copy jsonLoad into jsonSave, so the only entry is the one from tempText (awful name...).
    Maybe it would be simpler to drop jsonSave and to update jsonLoad instead.

  • edited February 2015

    Hello PhiLho, I know my choice of variable name is awe full :( sorry for that. Can you please tell me how do you want me to update the jsonLoad. I have seen setJSONArray() but it doesn't seems to work for me.

  • JSONArray has an append() method.

  • edited February 2015

    Please ignore ... I have solved the problem!

    Thanks PhiLho

    I just have to add the saveJSONArray(jsonLoad, "data/data.json"); just below the line number 29. :) :) :)

    //------------------------------------------------ Ahoi! PhiLho,

    Here I corrected the code and now I append the new JSONObject into the JSONArray and it is working but the problem is it doesn't save the value in to JSON file can you please tell me why it is happening?

    DateStamp D = new DateStamp();
    String saveText = " ";
    JSONArray jsonLoad;
    JSONObject tempTask;
    void setup() {
      size(400, 400, P3D);
      tempTask = new JSONObject();
      jsonLoad = loadJSONArray("data/data.json");
    }
    
    void draw() {
      background(-1);
      saveButton();
      fill(0);
      text(saveText, 10, 40 );
      //----------------
      for (int i = 0; i < jsonLoad.size (); i++) {
        JSONObject tempJSONObject = jsonLoad.getJSONObject(i); 
        int id = tempJSONObject.getInt("id");
        String date = tempJSONObject.getString("date");
        String task = tempJSONObject.getString("message");
        text(id + ", " + date + ", " + task, 10, 15+40*i);
      }
      //-----------------
      if (save) {
        tempTask.setInt("id", jsonLoad.size());
        tempTask.setString("date", D.createStamp());
        tempTask.setString("message", saveText);
        jsonLoad.append(tempTask);
        save = false;
      }
    }
    

    Rest of the code are same.

Sign In or Register to comment.