-3

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!

wonsuc
  • 3,498
  • 1
  • 27
  • 30
  • 1
    You could use GSON to serialize and deserialize JSON. – motiver Oct 21 '15 at 05:28
  • You can try Gson or Jackson for the same. – Vipul Asri Oct 21 '15 at 05:29
  • @motiver vipul_asri Can u guys gimme some example code? – wonsuc Oct 21 '15 at 05:32
  • There are a lot of tutorials on that: http://stackoverflow.com/questions/9605913/how-to-parse-json-in-android – m.aibin Oct 21 '15 at 05:43
  • @m.aibin I know how to use it, and used it before. But actually I'm using "JsonObjectRequest" of Volley library from Google. This class returns "org.json.JSONObject" for http response. Do I need to change this JSONObject to gson or jackson? And I asked best practice or example, not suggestion of libraries. – wonsuc Oct 21 '15 at 05:45
  • I used jackson a lot, and it's efficient library. But it all depend on your preferences, and you may use GSON – m.aibin Oct 21 '15 at 05:50
  • @m.aibin Ok, thanks. But I think I should handle "org.json.JSONObject" :( – wonsuc Oct 21 '15 at 05:53

3 Answers3

0

Have a look at this : Look at this : http://www.technotalkative.com/lazy-productive-android-developer-3/

Syed Usman
  • 300
  • 3
  • 8
  • I'm using "JsonObjectRequest" of Volley library from Google. This class returns "org.json.JSONObject" for http response. Do I need to change this JSONObject to gson or jackson? – wonsuc Oct 21 '15 at 05:41
  • Android's own JSON parsing capabilities are not present in apis before android 11, so GSON & Jackson were widely used. i think this might be what u need : http://www.technotalkative.com/android-json-parsing/ – Syed Usman Oct 21 '15 at 05:47
  • I think my app's min sdk is 11 and above so if it doesn't matter? – wonsuc Oct 21 '15 at 05:49
0

Checkout this simple and easy tutorial Getting Started with GSON.

you can include the GSON dependency in your project using Gradle :

compile 'com.google.code.gson:gson:2.3.1'
Vipul Asri
  • 8,903
  • 3
  • 46
  • 71
  • Thanks I read all of ur referenced url. And can u read my comment? [ I'm using "JsonObjectRequest" of Volley library from Google. This class returns "org.json.JSONObject" for http response. Do I need to change this JSONObject to gson or jackson? ] – wonsuc Oct 21 '15 at 05:55
  • no you don't have to use JSONObject or anything. just go through the above link you will be able to understand easily. – Vipul Asri Oct 21 '15 at 07:01
0

Just use Gson given your sample data. This method shows two different ways to get the json into Gson using either a String or a JSONObject

public class Main {

    public static void main(String[] args) {
        new Main().gsonExample();
    }

    /* Sample data
    {
    "text": "Hello World !",
    "site": "http://helloworld.com"
    }
     */
    private void gsonExample() {
        // sample data without the pretty printing
        String jsonText = "{\"text\": \"Hello World !\",\"site\": \"http://helloworld.com\"}";

        // manually convert
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("text", "Hello World !");
        jsonObject.addProperty("site", "http://helloworld.com");

        // assert the generated json is equal to the sample data
        assert jsonObject.toString().equals(jsonText);

        Model modelFromJsonObject = new Gson().fromJson(jsonObject.toString(), Model.class);
        Model model = new Gson().fromJson(jsonText, Model.class);
        // assert the two models are equals
        assert modelFromJsonObject.equals(model);

        // assert the text is set correctly
        assert  "Hello World !".equals(model.text);

        System.out.println("All tests passed!");
    }

    public static class Model {
        public String text;
        public String site;
    }
}
JBirdVegas
  • 10,855
  • 2
  • 44
  • 50