title: How to write into a json file in Android
body: <p>I am using a bundled json file in my application. I want to read and write data into the json file. I have a listview which displays the currently available data in json and an edittext into which user enters data. That data has to be written into the json. How to do this?</p>
  
  <p>This is my code...</p>
  
  <pre><code>  public class MainActivity extends Activity 
      {
          String myjsonstring;
  
           ArrayList&lt;Data&gt; web = new ArrayList&lt;Data&gt;();
  
           @Override
          protected void onCreate(Bundle savedInstanceState) 
          {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main);
  
              JsonParser();
  
              }
  
  
  
          private void JsonParser() 
          {
              // Reading text file from assets folder
              StringBuffer sb = new StringBuffer();
              BufferedReader br = null;
              try {
                  br = new BufferedReader(new InputStreamReader(getAssets().open(
                          "single.json")));
                  String temp;
                  while ((temp = br.readLine()) != null)
                      sb.append(temp);
              } catch (IOException e) {
                  e.printStackTrace();
              } finally {
                  try {
                      br.close(); // stop reading
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
  
              myjsonstring = sb.toString();
  
              // Try to parse JSON
              try {
  
  
                  JSONObject jsonObjMain = new JSONObject(myjsonstring);
                  JSONArray jsonArray = jsonObjMain.getJSONArray("message");
  
                  for (int i=0; i&lt;jsonArray.length(); i++) {
  
                      JSONObject jsonObj = jsonArray.getJSONObject(i);
  
                      // Getting data from individual JSONObject
  
                      Data data = new Data(jsonObj.getString("name") , jsonObj.getString("msg"));
  
                      web.add(data);
                  }
  
                  final customtest1 adapter = new customtest1(MainActivity.this,R.layout.list_single,web); 
                  final ListView list = (ListView)findViewById(R.id.list);
                  list.setAdapter(adapter);
  
                  list.setOnItemClickListener(new AdapterView.OnItemClickListener() 
                  {
  
                      @Override
                      public void onItemClick(AdapterView&lt;?&gt; parent, View view,
                              int position, long id) {
                           Toast.makeText(MainActivity.this, "TEST.........", Toast.LENGTH_SHORT).show();
                           adapter.notifyDataSetChanged();
                      }
  
                  });
                //Send activity......
  
                  final EditText et = (EditText)findViewById(R.id.EditText1);
                  final Button imb=(Button)findViewById(R.id.btn_send);
                  imb.setOnClickListener(new OnClickListener()
                  {
                   @Override
                   public void onClick(View arg0) 
                   { 
                        String str = et.getText().toString();
  
                        Data store = new Data(et.getText().toString());
                        web.add(store);
  
                         Toast.makeText(MainActivity.this, "You entered...."+store, Toast.LENGTH_SHORT).show();
                        et.setText(" ");
                        scrollMyListViewToBottom();
                        adapter.notifyDataSetChanged();
  
                          }
                  //Screen auto scrollup....
                   private void scrollMyListViewToBottom() {
                          list.post(new Runnable() {
                              @Override
                              public void run() {
                                  // Select the last row so it will scroll into view...
                                  list.setSelection(adapter.getCount() - 1);
                              }
                          });
                  }});
  
              }
              catch (JSONException e) 
              {
                  e.printStackTrace();
              }
             }
      }
  </code></pre>