Tuesday, March 6, 2012

Json Parsing in Android



Json URL :




String mFinalPassingUrl = "";
JSONObject mRetrievedJson = null;


String mReturnedString = "";

JSONObject mRetrievedJson = null;

mRetrievedJson = new JSONObject(mReturnedString);




private String parseJSONObject(JSONObject nJsonObject) {
JSONObject mResultObject = null;


int mFound = 0;
Log.e("log_tag", " Parse Json ");
try {
mResultObject = nJsonObject.getJSONObject("ResultSet");
mFound = mResultObject.getInt("Found");
if (mFound >= 1) {
JSONArray mPlacesArray = mResultObject.getJSONArray("Results");
JSONObject jobjData = mPlacesArray.getJSONObject(0);


if (jobjData.optString("line1") != null
&& !jobjData.optString("line1").equals("")) {
strFindAddress.append(jobjData.optString("line1") + ", ");
}
if (jobjData.optString("line2") != null
&& !jobjData.optString("line2").equals("")) {
strFindAddress.append(jobjData.optString("line2") + ", ");
}
if (jobjData.optString("city") != null
&& !jobjData.optString("city").equals("")) {
strFindAddress.append(jobjData.optString("city") + ", ");
}
if (jobjData.optString("county") != null
&& !jobjData.optString("county").equals("")) {
strFindAddress.append(jobjData.optString("county") + ", ");
}
if (jobjData.optString("state") != null
&& !jobjData.optString("state").equals("")) {
strFindAddress.append(jobjData.optString("state") + ", ");
}
if (jobjData.optString("statecode") != null
&& !jobjData.optString("statecode").equals("")) {
strFindAddress
.append(jobjData.optString("statecode") + ",");
}
if (jobjData.optString("country") != null
&& !jobjData.optString("country").equals("")) {
strFindAddress.append(jobjData.optString("country") + "-");
}
if (jobjData.optString("countrycode") != null
&& !jobjData.optString("countrycode").equals("")) {
strFindAddress.append(jobjData.optString("countrycode")
+ " \n ");
}
// if (jobjData.optString("country") != null
// && !jobjData.optString("country").equals("")) {
// strFindAddress.append(jobjData.optString("country")
// + " \n ");
// }
if (jobjData.optString("postal") != null
&& !jobjData.optString("postal").equals("")) {
strFindAddress.append("Zip Code - "
+ jobjData.optString("postal"));
}


if (jobjData.optString("latitude") != null
&& !jobjData.optString("latitude").equals("")) {
Editor prefsEditor = prefLatLong.edit();
prefsEditor
.putString("lat", jobjData.optString("latitude"));
prefsEditor.putString("long",
jobjData.optString("longitude"));
prefsEditor.commit();
}


Log.e("log_tag", "str find data " + strFindAddress);
} else {
Log.e("log_tag", "InValid data");


}
} catch (JSONException e) {
return String.valueOf(mFound);
}
return String.valueOf(mFound);


}

No comments:

Post a Comment