Friday, December 16, 2016

Converting JSON to Java Objcet by using Gson library





Download Gson library and put in your project build path.


Java Object will be a pojo ResponsePojo with fields status,code and message.

Code:
           
                 String jsonString = "{\"status\": 200,\ "code\": \"007\",\"message\": \"working fine                                                    \"}";
                Gson gson = new Gson();

//JSON to java object

jsonResponse = gson.toJson(jsonString );
                ResponsePojo rp = gson.fromJson(jsonResponse , ResponsePojo .class);



Output:

{
           "status": 200,
           "code": "007",
          "message": "working fine"
}


No comments:

Post a Comment

Fixing yum command on linux

You may find yourself having to fix more packages. So you can just remove everything you had installed via  pip and reinstall everythin...