简体   繁体   中英

How to check json for key and if it exists then set the value in java model Object

I have a model object which is initialized with default values. To refresh the content of object I call an web service and get the response and get the content from json object. I want to check If json response contains the object or not. If it does then call the setter and set the data and if it doesn't then leave then don't set it. I have approx 300 fields in my object. How I can do it with less code. I am listing my current approach.

  1. My Model object is like

     public class MyObject { private String str1 = "Initial Value1"; private String str2 = "Initial Value2"; public void setStr1(String str1) { this.str1 = str1; } public void setStr2(String str2) { this.str2 = str2; } public String getStr1(){ return str1; } public String getStr2(){ return str2; } } 

my json response be like

       {
     "val_one":"New Value1",
     "val_two":"New_value2"
      }

Now at run time I need to set the value from json response

       MyObject myObject = new MyObject();

       if(jsonObject.has("val_one"));
       myObject.setStr1(jsonObject.get("val_one"));
       if(jsonObject.has("val_two"));
       myObject.setStr2(jsonObject.get("val_two"));

Now how to do it in a better and efficient

If both sides are using JAVA then why not just use json-io. You can create an object as normal. ie

Animal a = new Aminmal() andimal.setName("bob");

Then use json-io to make it into json -- stream to where ever it needs to be... use json io to change back to object

This can be done using

JsonWriter.objectToJson(Object o);
JsonReader.jsonToJava(String json);

https://code.google.com/p/json-io/

json-io is also extremely light weight and quicker than most if not all other third party json library's that I have used.

That being said if you want to have more control on the output ie.. date conversions etc.. then look at GSON.

https://code.google.com/p/google-gson/

Another option, in addition to the other suggestions is gson. Here the link for gson information.

Essentially the idea with gson being that you define an object to represent the JSON structure that you are receiving. So somewhat like what you have now, you'd just need to change the object attributes to match the names of the JSON fields, ie 'val_one' and 'val_two'.

Then you just need to use gson to create the object from the JSON text, eg:

Gson gson = new GsonBuilder().create();
MyObject json = gson.fromJson(jsonStr, MyObject.class);

Why do you want to take of the object model mapping yourself? If you take spring then you can use the jackson mapper and have it all done for you. If you don't want to use spring then you still can use jackson2 and let it handle the parsing: http://wiki.fasterxml.com/JacksonRelease20

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM