flipkart

Monday, July 7, 2014

Creating JSON file programitically

JSON stands for Java Script Object Notation. It is user for data saving .Today this post explain creating JSON file and writing values inside file using android programming.


MainActivity.java


 package com.example.jsonwriter;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
JSONObject object = new JSONObject();
 try {
   object.put("name", "vamsi krishna");
   object.put("score", new Integer(44));
   object.put("current", new Double(40));
   object.put("nickname", "vk");
 } catch (JSONException e) {
   e.printStackTrace();
 }
 System.out.println("------"+object);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

AndroidManifest.xml, layout file are same after creating project change accordingly as your date in MainActivity class  

After running above my application it will give following screenshots


Above picture logcat showing out of application(Green color lines)


No comments:

Post a Comment