title: How to get and set JSONObject , JSONArray in J2ME
body: <p>I am new to JSON programming in J2ME.</p>
  
  <p>I discovered that  Json is used to exchange data much like XML.</p>
  
  <p>I want to know about the exchange in Array object from JSONtoObject and vice versa</p>
  
  <p>Below written is code where I convert from JSON to Object and vice versa.</p>
  
  <p>But I don't know about how to do for complex data structure like arrays etc.</p>
  
  <p>// App Loader </p>
  
  <pre><code>import javax.microedition.midlet.MIDlet;
  import javax.microedition.midlet.MIDletStateChangeException;
  
  
  public class AppLoader extends MIDlet {
  
      public AppLoader() {
          // TODO Auto-generated constructor stub
  
          // Converting Object to JSON
  
          UserData data=new UserData();
          data.setId(10);
          data.setName("Yatin");
          data.setDescription("Testing JSON in J2ME");
          System.out.println("Convert to JSON"+data.toJSON());
  
  
          //Convert JSON to Object
          String sample="{\"id\":99,\"name\":\"Tester\",\"description\":\"This is JSON Data\"}";
          UserData data2=new UserData();
          data2.fromJSON(sample);
          System.out.println("Convert from JSON "+data2);
      }
  
      protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
          // TODO Auto-generated method stub
  
      }
  
      protected void pauseApp() {
          // TODO Auto-generated method stub
  
      }
  
      protected void startApp() throws MIDletStateChangeException {
          // TODO Auto-generated method stub
  
      }
  
  }
  </code></pre>
  
  <p>In this class I created getters and setters for the String type of objects and then created JsonObject to create a JSON object to create a JSON object and then vice versa as per functions <code>toJSON()</code> and <code>fromJSON()</code></p>
  
  <p>// User Data class</p>
  
  <pre><code>import org.json.me.JSONException;
  import org.json.me.JSONObject;
  
  
  public class UserData {
      private int id;
      private String name;
      private String description;
  
      public int getId() {
          return id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public String getDescription() {
          return description;
      }
  
      public void setDescription(String description) {
          this.description = description;
      }
  
      public String toString()
      {
          return getId()+"-"+getName()+"-"+getDescription();
      }
  
  
  
      public String toJSON() {
          // TODO Auto-generated method stub
          JSONObject inner=new JSONObject();
  
          try {
              inner.put("id",getId());
              inner.put("description", getDescription());
              inner.put("name", getName());
          } catch (JSONException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
          return inner.toString();
      }
  
      public void fromJSON(String jsonString) {
          // TODO Auto-generated method stub
          try {
              JSONObject json=new JSONObject(jsonString);
              setId(json.getInt("id"));
              setDescription(json.getString("description"));
              setName(json.getString("name"));
          } catch (JSONException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
  
  
      }
  
  }
  </code></pre>
  
  <hr>
  
  <p>i found a better link for this question </p>
  
  <p><a href="http://jimmod.com/blog/2011/09/java-me-j2me-json-implementation-for-array-object/" rel="nofollow">http://jimmod.com/blog/2011/09/java-me-j2me-json-implementation-for-array-object/</a></p>