My android app has to parse several type of json texts, for exmple like below.
{
"text": "Hello World !",
"site": "http://helloworld.com"
}
So I want to make a class for managing every json texts which this app has to parse. like a "JsonUtil.java"
I made the class like this.
public class JsonUtil {
public static final String TAG = JsonUtil.class.getSimpleName();
public JsonUtil() {
}
public NotificationAd parseNotificationAd(JSONObject jsonObject) {
NotificationAd notificationAd = new NotificationAd();
try {
notificationAd.message = jsonObject.getString("text");
notificationAd.targetUrl = jsonObject.getString("site");
} catch (Exception e) {
e.printStackTrace();
}
Log.i(TAG, "parseNotificationAd, notificationAd: " + notificationAd.toString());
return notificationAd;
}
public class NotificationAd {
public String message;
public String targetUrl;
@Override
public String toString() {
return String.format("message: %s, targetUrl: %s", message, targetUrl);
}
}
}
The reason which I used nested class is too many "VO.java" classes could irritate total package structure(I don't know why, just my taste :P too many classes make me complicated.)
So the usage is like this,
JsonUtil.NotificationAd notificationAd = new JsonUtil().parseNotificationAd(response);
String message = notificationAd.message;
String targetUrl = notificationAd.targetUrl;
I'm wondering if I'm correct, actually I wanted make the class as "abstract", and make the method to "static" like below.
public abstract class JsonUtil {
public static final String TAG = JsonUtil.class.getSimpleName();
public static NotificationAd parseNotificationAd(JSONObject jsonObject) {
NotificationAd notificationAd = new NotificationAd();
try {
notificationAd.message = jsonObject.getString("text");
notificationAd.targetUrl = jsonObject.getString("site");
} catch (Exception e) {
e.printStackTrace();
}
Log.i(TAG, "parseNotificationAd, notificationAd: " + notificationAd.toString());
return notificationAd;
}
public static class NotificationAd {
public String message;
public String targetUrl;
@Override
public String toString() {
return String.format("message: %s, targetUrl: %s", message, targetUrl);
}
}
}
But I thought the next code has some memory issue(This point is what I need some help. I'm not professional for JAVA).
Can anybody suggest which is best practice for Json parser in android?
(I know Retrofit library, but plz don't mention it in this question :P)
Big thanks for every answers!