Android JSON Parser
JSON Parser or how do we parse JSON string from web services, for who never know what is JSON string is look like here you can see the example :
[{
"CreatedBy":2147483647,
"CreatedDate":"\/Date(928124400000+0700)\/",
"Employee_Id":2147483647,
"UpdatedBy":2147483647,
"UpdatedDate":"\/Date(928124400000+0700)\/",
"datBirdthDate":"\/Date(928124400000+0700)\/",
"datJointDate":"\/Date(928124400000+0700)\/",
"datResignDate":"\/Date(928124400000+0700)\/",
"intLevel":2147483647,
"strDepartmentCode":"String content",
"strDepartmentName":"String content",
"strDivisionCode":"String content",
"strDivisionName":"String content",
"strEmail_Address":"String content",
"strEmplName":"String content",
"strPhoneExt":"String content",
"strSAP_Code":"String content",
"strSMFNumber":"String content",
"strSectionCode":"String content",
"strSectionName":"String content",
"strSex":"String content",
"strShiftCode":"String content",
"strStatus":"String content"
}]
We can test some webservices that available in the internet by call the webservices with the parameter by typing webservices in browser address bar
For example :
In the example above I call “http://localhost/Login.svc/Section/400P”
Login.svc as the service and Section as webservice operation then 400P as the parameter
The JSON string as the result.
Okay let’s we call it every body now know what is JSON string look like , now let’s back to the Android JSON parser client I use client cause the android application will consume these JSON string for the application. And we will discuss how to parse the JSON string in android as the client of webservices.
And this is it for the procedure
public static String getJSON(String url) {
String json = “”;
String json = “”;
StringBuilder sb=new StringBuilder();
JSONObject jObj= new JSONObject();
int cp;
try {
URL requestUrl=new URL(url);
URLConnection con = requestUrl.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
while((cp=in.read())!=-1){
sb.append((char)cp);
json=sb.toString();
// jObj = new JSONObject(json);
}
}
catch(Exception e){
e.printStackTrace();
}
return json;
}
JSONObject jObj= new JSONObject();
int cp;
try {
URL requestUrl=new URL(url);
URLConnection con = requestUrl.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
while((cp=in.read())!=-1){
sb.append((char)cp);
json=sb.toString();
// jObj = new JSONObject(json);
}
}
catch(Exception e){
e.printStackTrace();
}
return json;
}
In getJSON procedure I pass String url as the url of webservice
Url: “http://localhost/Login.svc/Section/400P”
getJSON() use String as return the JSON string that we will use it with JSONObject or JSONArray it’s based on webservice JSON return.
For JSONObject we can use the JSON string like this:
JSONObject jObj;
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
and for JSONArray we can use it like this:
JSONArray jsonArray;
try {
jsonArray = new JSONArray(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
try {
jsonArray = new JSONArray(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
To get JSONObject from JSONArray we can use for conditional:
JSONArray androids = new JSONArray(json);
for(int i = 0; i < androids.length(); i++) {
JSONObject c = androids.getJSONObject(i);
}
for(int i = 0; i < androids.length(); i++) {
JSONObject c = androids.getJSONObject(i);
}
And for get the value of JSONObject we can get it by
public void setValue(JSONObject jsonObject){
try {
UserId = jsonObject.getInt("UserId");
StrUserName = jsonObject.getString("StrUserName");
StrPassword =jsonObject.getString("StrPassword");
StrDisplayName =jsonObject.getString("StrDisplayName");
StrStatus =jsonObject.getString("StrStatus");
strSAPCode =jsonObject.getString("strSAPCode");
// isEmpty="1";
} catch (Exception e) {
e.printStackTrace();
}
}
try {
UserId = jsonObject.getInt("UserId");
StrUserName = jsonObject.getString("StrUserName");
StrPassword =jsonObject.getString("StrPassword");
StrDisplayName =jsonObject.getString("StrDisplayName");
StrStatus =jsonObject.getString("StrStatus");
strSAPCode =jsonObject.getString("strSAPCode");
// isEmpty="1";
} catch (Exception e) {
e.printStackTrace();
}
}
Let see Sample explanation:
UserId = jsonObject.getInt("UserId");
The string “UserId” is the name variable that we get from JSONObject string.
{"CreatedBy":0,"CreatedDate":null,"DatInactived":null,"DatStartActive":null,"StrDisplayName":"M. SUBHAN AKBAR","StrPassword":"XDCXDVC","StrStatus":"A","StrUserName":"1125614","UpdatedBy":0,"UpdatedDate":null,"UserId":207,"strSAPCode":"1125614","ysnFirstLogin":null}
The value off “UserId” is 207
Okay guys and lads lets call it a day,
Just remember guys nobody is perfect and nobody is not perfect, we perfect cause we trying and we not perfect cause we did not trying, cause we never know what we capable without trying.
Stop thinking. just do it when we have trouble then we think . why it's become the trouble? then we will fix it together or by yourself
