In this blog I am going to explain How to parse a json file. Here I am going to explain how to parse a json file stored in Asset folder. Here my file name is user_details.json which stored in asset folder(json_file/user_details.json). Here is my json file.
"Name": "Shidhin TS",
"UserId" : "1",
"Website" : "http://shidhints.com",
"Qualification": [
{
"Name": "Name1",
"Institute": "Institute Name1",
"Year": "2006 - 2009",
"Mark": "87%"
},
{
"Name": "Name2",
"Institute": "Institute Name2",
"Year": "2004 - 2006",
"Mark": "78%"
},
{
"Name": "Name3",
"Institute": "Institute Name3",
"Year": "2004",
"Mark": "93%"
}
]
}
In this Json file I am having Strings Name, UserId, Website, Company, Mobile and an Array Qualification which is also contains some Strings.
try {
InputStream is = getApplicationContext().getAssets().open("json_file/user_details.json");
String jString = ReadInputStreamAsString(is);
JSONObject jObject = new JSONObject(jString);
//Extracting Strings
String nameStr = jObject.getString("Name"); // Extract Name
String userIdStr = jObject.getString("UserId"); // Extract UserId
String websiteStr = jObject.getString("Website"); // Extract Website
String companyStr = jObject.getString("Company"); // Extract Website
String mobileStr = jObject.getString("Mobile"); // Extract Mobile
//Extracting Qualification Array
JSONArray viewsObject = jObject.getJSONArray("Qualification");
for (int i = 0; i < viewsObject.length(); i++) {
JSONObject object = viewsObject.getJSONObject(i); //Creating Json object
String name = object.getString("Name");
String institute = object.getString("Institute");
String year = object.getString("Year");
String mark = object.getString("Mark");
}
} catch (Exception e) {
Helper.reportException(CreateCard.this, e);
}
public static String ReadInputStreamAsString(InputStream in) throws IOException {
BufferedInputStream bis = new BufferedInputStream(in);
ByteArrayOutputStream buf = new ByteArrayOutputStream();
int result = bis.read();
while (result != -1) {
byte b = (byte) result;
buf.write(b);
result = bis.read();
}
return buf.toString();
}
user_details.json
{"Name": "Shidhin TS",
"UserId" : "1",
"Website" : "http://shidhints.com",
"Company" : "B-Mates Technologies",
"Mobile" : "+91XXXXXXXXXX","Qualification": [
{
"Name": "Name1",
"Institute": "Institute Name1",
"Year": "2006 - 2009",
"Mark": "87%"
},
{
"Name": "Name2",
"Institute": "Institute Name2",
"Year": "2004 - 2006",
"Mark": "78%"
},
{
"Name": "Name3",
"Institute": "Institute Name3",
"Year": "2004",
"Mark": "93%"
}
]
}
In this Json file I am having Strings Name, UserId, Website, Company, Mobile and an Array Qualification which is also contains some Strings.
Java Code
Here we are parsing the json file with the help of JSONObject. First we will create a JSONObject and we will extract the String using .getString() method. Here is the sample code.
try {
InputStream is = getApplicationContext().getAssets().open("json_file/user_details.json");
String jString = ReadInputStreamAsString(is);
JSONObject jObject = new JSONObject(jString);
//Extracting Strings
String nameStr = jObject.getString("Name"); // Extract Name
String userIdStr = jObject.getString("UserId"); // Extract UserId
String websiteStr = jObject.getString("Website"); // Extract Website
String companyStr = jObject.getString("Company"); // Extract Website
String mobileStr = jObject.getString("Mobile"); // Extract Mobile
//Extracting Qualification Array
JSONArray viewsObject = jObject.getJSONArray("Qualification");
for (int i = 0; i < viewsObject.length(); i++) {
JSONObject object = viewsObject.getJSONObject(i); //Creating Json object
String name = object.getString("Name");
String institute = object.getString("Institute");
String year = object.getString("Year");
String mark = object.getString("Mark");
}
} catch (Exception e) {
Helper.reportException(CreateCard.this, e);
}
ReadInputStreamAsString
This Function will convert the given InputStream to a String. Using this string we will create a json Object to parse the json.public static String ReadInputStreamAsString(InputStream in) throws IOException {
BufferedInputStream bis = new BufferedInputStream(in);
ByteArrayOutputStream buf = new ByteArrayOutputStream();
int result = bis.read();
while (result != -1) {
byte b = (byte) result;
buf.write(b);
result = bis.read();
}
return buf.toString();
}