Sample JSON File which is stored in internal memory
{"STUDENTS":
[
{"SID":"123","NAME":"mahesh","ADDRESS":
{"STREET":"4- 4","VILLAGE":"velgodu"}
}
]
}
Similar file in XML for above json:
<STUDENTS>
<STUDENT>
<SID>123</SID>
<NAME>mahesh</NAME>
<ADDRESS>
<STREET>4-4</STREET>
<VILLAGE>velgodu</VILLAGE>
</ADDRESS>
</STUDENT>
</STUDENTS>
Write into internal memory:
public void write(View v)
{
JSONObject obj=new JSONObject();
JSONArray arr=new JSONArray();
try
{
JSONObject objsid=new JSONObject();
objsid.put("SID", "123");
objsid.put("NAME", "mahesh");
JSONObject objadd=new JSONObject();
objadd.put("STREET", "4-4");
objadd.put("VILLAGE", "velgodu");
objsid.put("ADDRESS", objadd);
arr.put(objsid);
obj.put("STUDENTS", arr);
FileWriter write=new FileWriter("storage/sdcard0/jsontext.txt");
write.write(obj.toString());
write.flush();
write.close();
}
catch (Exception e)
{
}
}
OUTPUT:
You can see this file in the local storage in following location storage/sdcard0/jsontext.txt
{"STUDENTS":
[
{"SID":"123","NAME":"mahesh","ADDRESS":
{"STREET":"4- 4","VILLAGE":"velgodu"}
}
]
}
Reading JSON File:
public void read(View v)
{
try
{
FileReader read=new FileReader("storage/sdcard0/jsontext.txt");
int i=read.read();
String msg="";
while(i!=-1)
{
msg=msg+(char)i;
i=read.read();
}
JSONObject obj=new JSONObject(msg);
JSONArray arr=obj.getJSONArray("STUDENTS");
int sid=0;
String sname="";
String address="";
for (int j = 0; j < arr.length(); j ++)
{
JSONObject objsid=arr.getJSONObject(j);
sid=objsid.getInt("SID");
sname=objsid.getString("NAME");
address=objsid.getString("ADDRESS");
JSONObject objad=new JSONObject(address);
String street=objad.getString("STREET");
String vill=objad.getString("VILLAGE");
/ /Toast.makeText(getApplicationContext(), sid+"\n name:"+sname+"\n address:"+street+", "+vill, 2000).show();
}
}
catch (Exception e)
{
// TODO: handle exception
}
}
{"STUDENTS":
[
{"SID":"123","NAME":"mahesh","ADDRESS":
{"STREET":"4- 4","VILLAGE":"velgodu"}
}
]
}
Similar file in XML for above json:
<STUDENTS>
<STUDENT>
<SID>123</SID>
<NAME>mahesh</NAME>
<ADDRESS>
<STREET>4-4</STREET>
<VILLAGE>velgodu</VILLAGE>
</ADDRESS>
</STUDENT>
</STUDENTS>
Write into internal memory:
public void write(View v)
{
JSONObject obj=new JSONObject();
JSONArray arr=new JSONArray();
try
{
JSONObject objsid=new JSONObject();
objsid.put("SID", "123");
objsid.put("NAME", "mahesh");
JSONObject objadd=new JSONObject();
objadd.put("STREET", "4-4");
objadd.put("VILLAGE", "velgodu");
objsid.put("ADDRESS", objadd);
arr.put(objsid);
obj.put("STUDENTS", arr);
FileWriter write=new FileWriter("storage/sdcard0/jsontext.txt");
write.write(obj.toString());
write.flush();
write.close();
}
catch (Exception e)
{
}
}
OUTPUT:
You can see this file in the local storage in following location storage/sdcard0/jsontext.txt
{"STUDENTS":
[
{"SID":"123","NAME":"mahesh","ADDRESS":
{"STREET":"4- 4","VILLAGE":"velgodu"}
}
]
}
Reading JSON File:
public void read(View v)
{
try
{
FileReader read=new FileReader("storage/sdcard0/jsontext.txt");
int i=read.read();
String msg="";
while(i!=-1)
{
msg=msg+(char)i;
i=read.read();
}
JSONObject obj=new JSONObject(msg);
JSONArray arr=obj.getJSONArray("STUDENTS");
int sid=0;
String sname="";
String address="";
for (int j = 0; j < arr.length(); j ++)
{
JSONObject objsid=arr.getJSONObject(j);
sid=objsid.getInt("SID");
sname=objsid.getString("NAME");
address=objsid.getString("ADDRESS");
JSONObject objad=new JSONObject(address);
String street=objad.getString("STREET");
String vill=objad.getString("VILLAGE");
/ /Toast.makeText(getApplicationContext(), sid+"\n name:"+sname+"\n address:"+street+", "+vill, 2000).show();
}
}
catch (Exception e)
{
// TODO: handle exception
}
}
No comments:
Post a Comment