Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Include Empty/Null Variable to Json while serialize with Gson Android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 4.35k
    Comment on it

    Are you using Gson for Serializing Json String from Java Bean/Model class? If yes then It may be useful for you.

    Gson ignore null/empty value while serialize Object to Json String. Like

    Gson gson = new Gson();
    String jsonData = gson.toJson(obj);
    

    In above code if obj has any empty/null String variable then Gson didn't write that variable to Json String.

    But some time we need that parameters in Json. GsonBuilder has serialize() method to serialize Null/Empty variable to Json String.

    Here is example code for serialize object with empty/null variable to Json String.

       Object obj = new Object();
        GsonBuilder gb = new GsonBuilder();
        gb.serializeNulls();
        Gson gson = gb.create();
        String jsonData = gson.toJson(obj);
    

    Happy Coding :D

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: