JAVA - XML and JSON issue: Array management<











up vote
0
down vote

favorite












I am programming Java after long time and i am just new to Json.
I found a strange issue but not sure if I am not understanding it correctly.



This is part of the XML file i am reading:




<Events>
<event comment="topota1" file="test1.mp4" id="0" time="1000"/>
<event comment="topota2" file="test2.mp4" id="1" time="2000"/>
<event comment="topota3" file="test3.mp4" id="2" time="3000"/>
<event comment="topota4" file="test4.mp4" id="3" time="4000"/>
</Events>



I did a Json structure to manage some events information:




package miCMS;
{***}
import org.json.JSONArray;
import org.json.JSONObject;
{***}
JSONObject Event = new JSONObject();
JSONArray EventArr = new JSONArray();
{***}
public class miPlayer{
{***}

public void addEvent(int Id, int Time, String File, String Comment) {
try {
Event.put("Id", Id);
Event.put("Time", Time);
Event.put("File", File);
Event.put("Comment", Comment);
EventArr.put(Event);
}catch ( Exception e ) {e.printStackTrace();}
}



In another Java file I am reading the XML file and asigning to my Json Array:




package miCMS;
{***}
public class miXMLParser{

public static miCMS.miPlayer ReadFromXML(String Path){
try {

DOMParser parser = new DOMParser();
parser.parse(Path);
Document doc = parser.getDocument();

///////////////////////////////////EVENTOS!!!!!!!!!!!!!!!!!!!!!!!!!!!!
NodeList childNodesEvent = doc.getElementsByTagName("event");

for (int x = 0; x < childNodesEvent.getLength(); x++ ) {
Node NodeEvent = childNodesEvent.item(x);

player.addEvent(x, 1000, "", "");

NamedNodeMap attrsEvent = NodeEvent.getAttributes();
for (int y = 0; y < attrsEvent.getLength(); y++ ) {
Node attrEvent = attrsEvent.item(y);
String AttribName = attrEvent.getNodeName();

switch (AttribName) {
case "id":
player.EventArr.getJSONObject(x).put("Id",Integer.parseInt(attrEvent.getNodeValue()));
System.out.println(">X:" + x + " // Id:" + player.EventArr.getJSONObject(x).getInt("Id"));
break;
case "time":
player.EventArr.getJSONObject(x).put("Time",Integer.parseInt(attrEvent.getNodeValue()));
break;
case "file":
player.EventArr.getJSONObject(x).put("File",attrEvent.getNodeValue());
break;
case "comment":
player.EventArr.getJSONObject(x).put("Comment",attrEvent.getNodeValue());
break;
}
}
}

System.out.println("DESPUES DEL FOR");
System.out.println(">>X:0" + " // Id:" + player.EventArr.getJSONObject(0).getInt("Id"));
System.out.println(">>X:1" + " // Id:" + player.EventArr.getJSONObject(1).getInt("Id"));
System.out.println(">>X:2" + " // Id:" + player.EventArr.getJSONObject(2).getInt("Id"));
System.out.println(">>X:3" + " // Id:" + player.EventArr.getJSONObject(3).getInt("Id"));

}catch (Exception e) {
e.printStackTrace();
}

return player;
}



for some reason, all the elements in my JSON array is storing the information coming form the last element in my XML file.
I forced some printlines to see what is happening but is strange:



>X:0 // Id:0
>X:1 // Id:1
>X:2 // Id:2
>X:3 // Id:3
DESPUES DEL FOR
>>X:0 // Id:3
>>X:1 // Id:3
>>X:2 // Id:3
>>X:3 // Id:3


My question is: Why the info is being stored ok when inside the For loop but not when outside the For loop??



Any help is appreciated!










share|improve this question




























    up vote
    0
    down vote

    favorite












    I am programming Java after long time and i am just new to Json.
    I found a strange issue but not sure if I am not understanding it correctly.



    This is part of the XML file i am reading:




    <Events>
    <event comment="topota1" file="test1.mp4" id="0" time="1000"/>
    <event comment="topota2" file="test2.mp4" id="1" time="2000"/>
    <event comment="topota3" file="test3.mp4" id="2" time="3000"/>
    <event comment="topota4" file="test4.mp4" id="3" time="4000"/>
    </Events>



    I did a Json structure to manage some events information:




    package miCMS;
    {***}
    import org.json.JSONArray;
    import org.json.JSONObject;
    {***}
    JSONObject Event = new JSONObject();
    JSONArray EventArr = new JSONArray();
    {***}
    public class miPlayer{
    {***}

    public void addEvent(int Id, int Time, String File, String Comment) {
    try {
    Event.put("Id", Id);
    Event.put("Time", Time);
    Event.put("File", File);
    Event.put("Comment", Comment);
    EventArr.put(Event);
    }catch ( Exception e ) {e.printStackTrace();}
    }



    In another Java file I am reading the XML file and asigning to my Json Array:




    package miCMS;
    {***}
    public class miXMLParser{

    public static miCMS.miPlayer ReadFromXML(String Path){
    try {

    DOMParser parser = new DOMParser();
    parser.parse(Path);
    Document doc = parser.getDocument();

    ///////////////////////////////////EVENTOS!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    NodeList childNodesEvent = doc.getElementsByTagName("event");

    for (int x = 0; x < childNodesEvent.getLength(); x++ ) {
    Node NodeEvent = childNodesEvent.item(x);

    player.addEvent(x, 1000, "", "");

    NamedNodeMap attrsEvent = NodeEvent.getAttributes();
    for (int y = 0; y < attrsEvent.getLength(); y++ ) {
    Node attrEvent = attrsEvent.item(y);
    String AttribName = attrEvent.getNodeName();

    switch (AttribName) {
    case "id":
    player.EventArr.getJSONObject(x).put("Id",Integer.parseInt(attrEvent.getNodeValue()));
    System.out.println(">X:" + x + " // Id:" + player.EventArr.getJSONObject(x).getInt("Id"));
    break;
    case "time":
    player.EventArr.getJSONObject(x).put("Time",Integer.parseInt(attrEvent.getNodeValue()));
    break;
    case "file":
    player.EventArr.getJSONObject(x).put("File",attrEvent.getNodeValue());
    break;
    case "comment":
    player.EventArr.getJSONObject(x).put("Comment",attrEvent.getNodeValue());
    break;
    }
    }
    }

    System.out.println("DESPUES DEL FOR");
    System.out.println(">>X:0" + " // Id:" + player.EventArr.getJSONObject(0).getInt("Id"));
    System.out.println(">>X:1" + " // Id:" + player.EventArr.getJSONObject(1).getInt("Id"));
    System.out.println(">>X:2" + " // Id:" + player.EventArr.getJSONObject(2).getInt("Id"));
    System.out.println(">>X:3" + " // Id:" + player.EventArr.getJSONObject(3).getInt("Id"));

    }catch (Exception e) {
    e.printStackTrace();
    }

    return player;
    }



    for some reason, all the elements in my JSON array is storing the information coming form the last element in my XML file.
    I forced some printlines to see what is happening but is strange:



    >X:0 // Id:0
    >X:1 // Id:1
    >X:2 // Id:2
    >X:3 // Id:3
    DESPUES DEL FOR
    >>X:0 // Id:3
    >>X:1 // Id:3
    >>X:2 // Id:3
    >>X:3 // Id:3


    My question is: Why the info is being stored ok when inside the For loop but not when outside the For loop??



    Any help is appreciated!










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am programming Java after long time and i am just new to Json.
      I found a strange issue but not sure if I am not understanding it correctly.



      This is part of the XML file i am reading:




      <Events>
      <event comment="topota1" file="test1.mp4" id="0" time="1000"/>
      <event comment="topota2" file="test2.mp4" id="1" time="2000"/>
      <event comment="topota3" file="test3.mp4" id="2" time="3000"/>
      <event comment="topota4" file="test4.mp4" id="3" time="4000"/>
      </Events>



      I did a Json structure to manage some events information:




      package miCMS;
      {***}
      import org.json.JSONArray;
      import org.json.JSONObject;
      {***}
      JSONObject Event = new JSONObject();
      JSONArray EventArr = new JSONArray();
      {***}
      public class miPlayer{
      {***}

      public void addEvent(int Id, int Time, String File, String Comment) {
      try {
      Event.put("Id", Id);
      Event.put("Time", Time);
      Event.put("File", File);
      Event.put("Comment", Comment);
      EventArr.put(Event);
      }catch ( Exception e ) {e.printStackTrace();}
      }



      In another Java file I am reading the XML file and asigning to my Json Array:




      package miCMS;
      {***}
      public class miXMLParser{

      public static miCMS.miPlayer ReadFromXML(String Path){
      try {

      DOMParser parser = new DOMParser();
      parser.parse(Path);
      Document doc = parser.getDocument();

      ///////////////////////////////////EVENTOS!!!!!!!!!!!!!!!!!!!!!!!!!!!!
      NodeList childNodesEvent = doc.getElementsByTagName("event");

      for (int x = 0; x < childNodesEvent.getLength(); x++ ) {
      Node NodeEvent = childNodesEvent.item(x);

      player.addEvent(x, 1000, "", "");

      NamedNodeMap attrsEvent = NodeEvent.getAttributes();
      for (int y = 0; y < attrsEvent.getLength(); y++ ) {
      Node attrEvent = attrsEvent.item(y);
      String AttribName = attrEvent.getNodeName();

      switch (AttribName) {
      case "id":
      player.EventArr.getJSONObject(x).put("Id",Integer.parseInt(attrEvent.getNodeValue()));
      System.out.println(">X:" + x + " // Id:" + player.EventArr.getJSONObject(x).getInt("Id"));
      break;
      case "time":
      player.EventArr.getJSONObject(x).put("Time",Integer.parseInt(attrEvent.getNodeValue()));
      break;
      case "file":
      player.EventArr.getJSONObject(x).put("File",attrEvent.getNodeValue());
      break;
      case "comment":
      player.EventArr.getJSONObject(x).put("Comment",attrEvent.getNodeValue());
      break;
      }
      }
      }

      System.out.println("DESPUES DEL FOR");
      System.out.println(">>X:0" + " // Id:" + player.EventArr.getJSONObject(0).getInt("Id"));
      System.out.println(">>X:1" + " // Id:" + player.EventArr.getJSONObject(1).getInt("Id"));
      System.out.println(">>X:2" + " // Id:" + player.EventArr.getJSONObject(2).getInt("Id"));
      System.out.println(">>X:3" + " // Id:" + player.EventArr.getJSONObject(3).getInt("Id"));

      }catch (Exception e) {
      e.printStackTrace();
      }

      return player;
      }



      for some reason, all the elements in my JSON array is storing the information coming form the last element in my XML file.
      I forced some printlines to see what is happening but is strange:



      >X:0 // Id:0
      >X:1 // Id:1
      >X:2 // Id:2
      >X:3 // Id:3
      DESPUES DEL FOR
      >>X:0 // Id:3
      >>X:1 // Id:3
      >>X:2 // Id:3
      >>X:3 // Id:3


      My question is: Why the info is being stored ok when inside the For loop but not when outside the For loop??



      Any help is appreciated!










      share|improve this question















      I am programming Java after long time and i am just new to Json.
      I found a strange issue but not sure if I am not understanding it correctly.



      This is part of the XML file i am reading:




      <Events>
      <event comment="topota1" file="test1.mp4" id="0" time="1000"/>
      <event comment="topota2" file="test2.mp4" id="1" time="2000"/>
      <event comment="topota3" file="test3.mp4" id="2" time="3000"/>
      <event comment="topota4" file="test4.mp4" id="3" time="4000"/>
      </Events>



      I did a Json structure to manage some events information:




      package miCMS;
      {***}
      import org.json.JSONArray;
      import org.json.JSONObject;
      {***}
      JSONObject Event = new JSONObject();
      JSONArray EventArr = new JSONArray();
      {***}
      public class miPlayer{
      {***}

      public void addEvent(int Id, int Time, String File, String Comment) {
      try {
      Event.put("Id", Id);
      Event.put("Time", Time);
      Event.put("File", File);
      Event.put("Comment", Comment);
      EventArr.put(Event);
      }catch ( Exception e ) {e.printStackTrace();}
      }



      In another Java file I am reading the XML file and asigning to my Json Array:




      package miCMS;
      {***}
      public class miXMLParser{

      public static miCMS.miPlayer ReadFromXML(String Path){
      try {

      DOMParser parser = new DOMParser();
      parser.parse(Path);
      Document doc = parser.getDocument();

      ///////////////////////////////////EVENTOS!!!!!!!!!!!!!!!!!!!!!!!!!!!!
      NodeList childNodesEvent = doc.getElementsByTagName("event");

      for (int x = 0; x < childNodesEvent.getLength(); x++ ) {
      Node NodeEvent = childNodesEvent.item(x);

      player.addEvent(x, 1000, "", "");

      NamedNodeMap attrsEvent = NodeEvent.getAttributes();
      for (int y = 0; y < attrsEvent.getLength(); y++ ) {
      Node attrEvent = attrsEvent.item(y);
      String AttribName = attrEvent.getNodeName();

      switch (AttribName) {
      case "id":
      player.EventArr.getJSONObject(x).put("Id",Integer.parseInt(attrEvent.getNodeValue()));
      System.out.println(">X:" + x + " // Id:" + player.EventArr.getJSONObject(x).getInt("Id"));
      break;
      case "time":
      player.EventArr.getJSONObject(x).put("Time",Integer.parseInt(attrEvent.getNodeValue()));
      break;
      case "file":
      player.EventArr.getJSONObject(x).put("File",attrEvent.getNodeValue());
      break;
      case "comment":
      player.EventArr.getJSONObject(x).put("Comment",attrEvent.getNodeValue());
      break;
      }
      }
      }

      System.out.println("DESPUES DEL FOR");
      System.out.println(">>X:0" + " // Id:" + player.EventArr.getJSONObject(0).getInt("Id"));
      System.out.println(">>X:1" + " // Id:" + player.EventArr.getJSONObject(1).getInt("Id"));
      System.out.println(">>X:2" + " // Id:" + player.EventArr.getJSONObject(2).getInt("Id"));
      System.out.println(">>X:3" + " // Id:" + player.EventArr.getJSONObject(3).getInt("Id"));

      }catch (Exception e) {
      e.printStackTrace();
      }

      return player;
      }



      for some reason, all the elements in my JSON array is storing the information coming form the last element in my XML file.
      I forced some printlines to see what is happening but is strange:



      >X:0 // Id:0
      >X:1 // Id:1
      >X:2 // Id:2
      >X:3 // Id:3
      DESPUES DEL FOR
      >>X:0 // Id:3
      >>X:1 // Id:3
      >>X:2 // Id:3
      >>X:3 // Id:3


      My question is: Why the info is being stored ok when inside the For loop but not when outside the For loop??



      Any help is appreciated!







      java arrays json xml






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 at 16:55









      Hovercraft Full Of Eels

      260k20211317




      260k20211317










      asked Nov 22 at 16:12









      Acalleja

      33




      33
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          JSONObject::put overrides keys, therefore, as your JSONObject Event is a class field, everytime you call miPlayer::addEvent it stores the very same Event object in a new cell of EventArr, updating all the other cells (because each cell contains a pointer to your Event object). To fix this, simply use a local variable :



          package miCMS;
          {***}
          import org.json.JSONArray;
          import org.json.JSONObject;
          {***}
          JSONArray eventArr = new JSONArray();
          {***}
          public class MiPlayer{
          {***}

          public void addEvent(int id, int time, String file, String comment) {
          try {
          JSONObject event = new JSONObject();
          event.put("Id", id);
          event.put("Time", time);
          event.put("File", file);
          event.put("Comment", comment);
          eventArr.put(event);
          }catch ( Exception e ) {e.printStackTrace();}
          }


          By the way, Java Naming Conventions preconize to name variables with a lowercase first letter, and classes with a uppercase first letter :)






          share|improve this answer

















          • 1




            Thanks!! it seems that was the mistake.... I hope the example can help other people
            – Acalleja
            Nov 22 at 16:37










          • You're welcome, hope too !
            – TheWildHealer
            Nov 22 at 16:48


















          up vote
          0
          down vote













          Problem is in this line:



           EventArr.put(Event);


          each time you invoke 'addEvent' your code updates object 'Event', and because of that at the end you see values added last. You should create new instance every time



          public void addEvent(int Id, int Time, String File, String Comment) {
          try {
          Event = new JSONObject();
          Event.put("Id", Id);
          Event.put("Time", Time);
          Event.put("File", File);
          Event.put("Comment", Comment);
          EventArr.put(Event);
          }catch ( Exception e ) {e.printStackTrace();}
          }





          share|improve this answer





















          • Thanks! it is working now!!
            – Acalleja
            Nov 22 at 16:39











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434796%2fjava-xml-and-json-issue-array-management%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          JSONObject::put overrides keys, therefore, as your JSONObject Event is a class field, everytime you call miPlayer::addEvent it stores the very same Event object in a new cell of EventArr, updating all the other cells (because each cell contains a pointer to your Event object). To fix this, simply use a local variable :



          package miCMS;
          {***}
          import org.json.JSONArray;
          import org.json.JSONObject;
          {***}
          JSONArray eventArr = new JSONArray();
          {***}
          public class MiPlayer{
          {***}

          public void addEvent(int id, int time, String file, String comment) {
          try {
          JSONObject event = new JSONObject();
          event.put("Id", id);
          event.put("Time", time);
          event.put("File", file);
          event.put("Comment", comment);
          eventArr.put(event);
          }catch ( Exception e ) {e.printStackTrace();}
          }


          By the way, Java Naming Conventions preconize to name variables with a lowercase first letter, and classes with a uppercase first letter :)






          share|improve this answer

















          • 1




            Thanks!! it seems that was the mistake.... I hope the example can help other people
            – Acalleja
            Nov 22 at 16:37










          • You're welcome, hope too !
            – TheWildHealer
            Nov 22 at 16:48















          up vote
          0
          down vote



          accepted










          JSONObject::put overrides keys, therefore, as your JSONObject Event is a class field, everytime you call miPlayer::addEvent it stores the very same Event object in a new cell of EventArr, updating all the other cells (because each cell contains a pointer to your Event object). To fix this, simply use a local variable :



          package miCMS;
          {***}
          import org.json.JSONArray;
          import org.json.JSONObject;
          {***}
          JSONArray eventArr = new JSONArray();
          {***}
          public class MiPlayer{
          {***}

          public void addEvent(int id, int time, String file, String comment) {
          try {
          JSONObject event = new JSONObject();
          event.put("Id", id);
          event.put("Time", time);
          event.put("File", file);
          event.put("Comment", comment);
          eventArr.put(event);
          }catch ( Exception e ) {e.printStackTrace();}
          }


          By the way, Java Naming Conventions preconize to name variables with a lowercase first letter, and classes with a uppercase first letter :)






          share|improve this answer

















          • 1




            Thanks!! it seems that was the mistake.... I hope the example can help other people
            – Acalleja
            Nov 22 at 16:37










          • You're welcome, hope too !
            – TheWildHealer
            Nov 22 at 16:48













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          JSONObject::put overrides keys, therefore, as your JSONObject Event is a class field, everytime you call miPlayer::addEvent it stores the very same Event object in a new cell of EventArr, updating all the other cells (because each cell contains a pointer to your Event object). To fix this, simply use a local variable :



          package miCMS;
          {***}
          import org.json.JSONArray;
          import org.json.JSONObject;
          {***}
          JSONArray eventArr = new JSONArray();
          {***}
          public class MiPlayer{
          {***}

          public void addEvent(int id, int time, String file, String comment) {
          try {
          JSONObject event = new JSONObject();
          event.put("Id", id);
          event.put("Time", time);
          event.put("File", file);
          event.put("Comment", comment);
          eventArr.put(event);
          }catch ( Exception e ) {e.printStackTrace();}
          }


          By the way, Java Naming Conventions preconize to name variables with a lowercase first letter, and classes with a uppercase first letter :)






          share|improve this answer












          JSONObject::put overrides keys, therefore, as your JSONObject Event is a class field, everytime you call miPlayer::addEvent it stores the very same Event object in a new cell of EventArr, updating all the other cells (because each cell contains a pointer to your Event object). To fix this, simply use a local variable :



          package miCMS;
          {***}
          import org.json.JSONArray;
          import org.json.JSONObject;
          {***}
          JSONArray eventArr = new JSONArray();
          {***}
          public class MiPlayer{
          {***}

          public void addEvent(int id, int time, String file, String comment) {
          try {
          JSONObject event = new JSONObject();
          event.put("Id", id);
          event.put("Time", time);
          event.put("File", file);
          event.put("Comment", comment);
          eventArr.put(event);
          }catch ( Exception e ) {e.printStackTrace();}
          }


          By the way, Java Naming Conventions preconize to name variables with a lowercase first letter, and classes with a uppercase first letter :)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 at 16:33









          TheWildHealer

          405216




          405216








          • 1




            Thanks!! it seems that was the mistake.... I hope the example can help other people
            – Acalleja
            Nov 22 at 16:37










          • You're welcome, hope too !
            – TheWildHealer
            Nov 22 at 16:48














          • 1




            Thanks!! it seems that was the mistake.... I hope the example can help other people
            – Acalleja
            Nov 22 at 16:37










          • You're welcome, hope too !
            – TheWildHealer
            Nov 22 at 16:48








          1




          1




          Thanks!! it seems that was the mistake.... I hope the example can help other people
          – Acalleja
          Nov 22 at 16:37




          Thanks!! it seems that was the mistake.... I hope the example can help other people
          – Acalleja
          Nov 22 at 16:37












          You're welcome, hope too !
          – TheWildHealer
          Nov 22 at 16:48




          You're welcome, hope too !
          – TheWildHealer
          Nov 22 at 16:48












          up vote
          0
          down vote













          Problem is in this line:



           EventArr.put(Event);


          each time you invoke 'addEvent' your code updates object 'Event', and because of that at the end you see values added last. You should create new instance every time



          public void addEvent(int Id, int Time, String File, String Comment) {
          try {
          Event = new JSONObject();
          Event.put("Id", Id);
          Event.put("Time", Time);
          Event.put("File", File);
          Event.put("Comment", Comment);
          EventArr.put(Event);
          }catch ( Exception e ) {e.printStackTrace();}
          }





          share|improve this answer





















          • Thanks! it is working now!!
            – Acalleja
            Nov 22 at 16:39















          up vote
          0
          down vote













          Problem is in this line:



           EventArr.put(Event);


          each time you invoke 'addEvent' your code updates object 'Event', and because of that at the end you see values added last. You should create new instance every time



          public void addEvent(int Id, int Time, String File, String Comment) {
          try {
          Event = new JSONObject();
          Event.put("Id", Id);
          Event.put("Time", Time);
          Event.put("File", File);
          Event.put("Comment", Comment);
          EventArr.put(Event);
          }catch ( Exception e ) {e.printStackTrace();}
          }





          share|improve this answer





















          • Thanks! it is working now!!
            – Acalleja
            Nov 22 at 16:39













          up vote
          0
          down vote










          up vote
          0
          down vote









          Problem is in this line:



           EventArr.put(Event);


          each time you invoke 'addEvent' your code updates object 'Event', and because of that at the end you see values added last. You should create new instance every time



          public void addEvent(int Id, int Time, String File, String Comment) {
          try {
          Event = new JSONObject();
          Event.put("Id", Id);
          Event.put("Time", Time);
          Event.put("File", File);
          Event.put("Comment", Comment);
          EventArr.put(Event);
          }catch ( Exception e ) {e.printStackTrace();}
          }





          share|improve this answer












          Problem is in this line:



           EventArr.put(Event);


          each time you invoke 'addEvent' your code updates object 'Event', and because of that at the end you see values added last. You should create new instance every time



          public void addEvent(int Id, int Time, String File, String Comment) {
          try {
          Event = new JSONObject();
          Event.put("Id", Id);
          Event.put("Time", Time);
          Event.put("File", File);
          Event.put("Comment", Comment);
          EventArr.put(Event);
          }catch ( Exception e ) {e.printStackTrace();}
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 at 16:36









          n1cr4m

          12614




          12614












          • Thanks! it is working now!!
            – Acalleja
            Nov 22 at 16:39


















          • Thanks! it is working now!!
            – Acalleja
            Nov 22 at 16:39
















          Thanks! it is working now!!
          – Acalleja
          Nov 22 at 16:39




          Thanks! it is working now!!
          – Acalleja
          Nov 22 at 16:39


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434796%2fjava-xml-and-json-issue-array-management%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          What visual should I use to simply compare current year value vs last year in Power BI desktop

          How to ignore python UserWarning in pytest?

          Alexandru Averescu