Convert Java Complex Object to Json

I need to convert the following class:

package comS309.traxz.data; import java.util.Collection; import org.json.JSONException; import org.json.JSONObject; public class ExerciseSession { public String DateCreated; public String TotalTime; public String CaloriesBurned; public String AvgSpeed; public String SessionName; public String Distance; public String SessionType; public String UserId; public Collection<LatLon> LatLons; } 

Where LatLon is as follows:

 public class LatLon { public String LatLonId; public String Latitude; public String Longitude; public String ExerciseSessionId; public String LLAveSpeed; public String Distance; } 

Thus, the ExerciseSession class has a collection of LatLon objects. Now I need to convert the ExerciseSession class to Json format from java and send it to my server.

I do this on Android OS, if that matters.

My current solution is this:

 JSONObject ExerciseSessionJSOBJ = new JSONObject(); ExerciseSessionJSOBJ.put("DateCreated", this.DateCreated); ExerciseSessionJSOBJ.put("TotalTime", this.TotalTime); ExerciseSessionJSOBJ.put("CaloriesBurned", this.CaloriesBurned); ExerciseSessionJSOBJ.put("AvgSpeed", this.AvgSpeed); ExerciseSessionJSOBJ.put("SessionName", this.SessionName); ExerciseSessionJSOBJ.put("Distance", this.Distance); ExerciseSessionJSOBJ.put("SessionType", this.SessionType); ExerciseSessionJSOBJ.put("UserId", this.UserId); //add the collection for(LatLon l: LatLons) { ExerciseSessionJSOBJ.accumulate("LatLons", l); } 

I am not sure what this is really. I am new to Json and need help. Thank you in advance!

+6
source share
4 answers

This is very easy to do using the Google GSON library. An example is used here:

 Gson gson = new Gson(); String jsonRepresentation = gson.toJson(myComplexObject); 

And to return the object:

 Gson gson = new Gson(); MyComplexObject myComplexObject = gson.fromJson(jsonRepresentation, MyComplexObject.class); 

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

+16
source

You can also serialize an object using flexjson: http://flexjson.sourceforge.net/

+2
source

I think the use of the cluster is correct. See: http://www.json.org/javadoc/org/json/JSONObject.html#accumulate(java.lang.String,%20java.lang.Object )

But you need to create a JSONObject for each LatLon, as well as for the ExerciseSession object. Then the following line is incorrect: ExerciseSessionJSOBJ.accumulate ("LatLons", l);

"l" needs to be converted.

+1
source

I really suggest you avoid using JSONObject to convert between strings and Java objects. This will probably require your sanity if you need to do too much. Alternatively, I'm a big Jackson fan who does what you describe in a very enjoyable and easy way.

As a basic example,

 public static class LatLon { public final String LatLonId; public final String Latitude; public final String Longitude; public final String ExerciseSessionId; public final String LLAveSpeed; public final String Distance; @JsonCreator public LatLon(@JsonProperty("distance") String distance, @JsonProperty("exerciseSessionId") String exerciseSessionId, @JsonProperty("latitude") String latitude, @JsonProperty("latLonId") String latLonId, @JsonProperty("LLAveSpeed") String LLAveSpeed, @JsonProperty("longitude") String longitude) { this.Distance = distance; this.ExerciseSessionId = exerciseSessionId; this.Latitude = latitude; this.LatLonId = latLonId; this.LLAveSpeed = LLAveSpeed; this.Longitude = longitude; } public static void something() { ObjectMapper mapper = new ObjectMapper(); String json = "{\"LLAveSpeed\":\"123\",\"Distance\":\"123\"," + "\"ExerciseSessionId\":\"123\",\"LatLonId\":\"123\"," + "\"Latitude\":\"123\",\"Longitude\":\"123\"}"; try { //turn the json string into a LatLon object. LatLon latLon = mapper.readValue(json, LatLon.class); //turn the latLon object into a new JSON string String newJson = mapper.writeValueAsString(latLon); //confirm that the strings are equal Log.w("JacksonDemo", "Are they equal? " + json.equals(newJson)); } catch (IOException e) { e.printStackTrace(); } } } 

This outputs Are they equal? true Are they equal? true

So you use readValue() to convert json to a Java object, writeValueAsString() to write the object back to json. @JsonCreator denotes the constructor that Jackson should use to convert betwee json and Java. @JsonProperty("jsonKeyName") denotes the name of the variable in the json string and the Java variable to which it should map.

This is a bit confusing at first, but it saves a lot of time once you figure it out. Let me know if something is unclear.

+1
source

Source: https://habr.com/ru/post/902784/


All Articles