How to check json for key and if it exists then set the value in java model Object -
I have a model object, which to refresh the content is started with the default value object I Call web service and get feedback and get content from Jason object. I want to check if Jason is an object in response or if this happens then call the Setter and set the data and if it does not leave, do not set it. I have about 300 fields in my object, how can I do it with less code. I am listing my current perspective.
-
My model object is the same
public class MyObject {personal string str1 = "initial value 1"; Private string str2 = "initial value 2"; Public Zero Setset 1 (string straight 1) {this.str1 = str1; } Public Zero setStr2 (string str2) {this.str2 = str2; } Public string getStr1 () {return str1; } Public string getStr2 () {return str2; }
new value 1 "," val_two ":" New_value2 "}
now you need to set me value Jesn feedback at run time
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"));
how can it better and efficient to
If both sides are using Java, then just sub-jsn-oo Do not use. You can create an object as normal or
Animal a = new Aminml () andimal.setName ( "Bob");
< p> then JSON-IO should make use of it in Jason-stream where ever be ... using json IO to change back to the object this
can be done using JsonWriter.objectToJson (object o); JasonRedder Jason Totojwa (String JSN);
json-Io is also very light weight and is much faster if the other third party is Jason Library which I have used.
It is being said that if you want more control over output ie date conversion etc. see GSON.
Comments
Post a Comment