I'm consuming an external API, for which a JSON Object is returned. Contained in that Object response is an array that I need to extract and set to a List of a particular entity type. Java, however, is not a language that I'm very familiar with, so I'm having problems attempting to figure this out.
I've created a type of wrapper class to work with this in the setter.
The best I've come up with that compiles is below, but produces an error that I can't figure out.
public void setFlights(Object responseBody) {
String responseString = responseBody.toString();
JSONObject responseJSONObject = new JSONObject(responseString);
JSONArray responseJSONArray = responseJSONObject.getJSONArray("flights");
Gson gson = new Gson();
Type flightType = new TypeToken<List<Flight>>() {}.getType();
this.flights = gson.fromJson(String.valueOf(responseJSONArray), flightType);
}
As you can see, I'm kind of throwing it at the wall to see if it will stick. I'm trying to use Gson to get around some of the Type issues I've come across.
The error produced when executing is:
org.json.JSONException: Expected a ':' after a key at 7 [character 8 line 1]
Response String is as follows:
Response String Image
If that's difficult to deal with, here's the response in text:
INFO: {"FlightInfoResult":{"next_offset":-1,"flights":[{"ident":"N1RJ","aircrafttype":"HDJT","filed_ete":"01:25:00","filed_time":1593038253,"filed_departuretime":1593037500,"filed_airspeed_kts":400,"filed_airspeed_mach":"","filed_altitude":360,"route":"WEAZL4 CLAWD","actualdeparturetime":1593038285,"estimatedarrivaltime":1593043320,"actualarrivaltime":1593043320,"diverted":"","origin":"KJQF","destination":"KJXN","originName":"Concord-Padgett Rgnl","originCity":"Concord, NC","destinationName":"Jackson County","destinationCity":"Jackson, MI"},{"ident":"N1RJ","aircrafttype":"HDJT","filed_ete":"01:24:00","filed_time":1593000320,"filed_departuretime":1592998200,"filed_airspeed_kts":400,"filed_airspeed_mach":"","filed_altitude":350,"route":"PEGTE","actualdeparturetime":1593000420,"estimatedarrivaltime":1593005149,"actualarrivaltime":1593005149,"diverted":"","origin":"KJXN","destination":"KJQF","originName":"Jackson County","originCity":"Jackson, MI","destinationName":"Concord-Padgett Rgnl","destinationCity":"Concord, NC"},{"ident":"N1RJ","aircrafttype":"HDJT","filed_ete":"01:29:00","filed_time":1592518049,"filed_departuretime":1592513400,"filed_airspeed_kts":317,"filed_airspeed_mach":"","filed_altitude":360,"route":"WEAZL4 CLAWD","actualdeparturetime":1592517936,"estimatedarrivaltime":1592523120,"actualarrivaltime":1592523120,"diverted":"","origin":"KJQF","destination":"KJXN","originName":"Concord-Padgett Rgnl","originCity":"Concord, NC","destinationName":"Jackson County","destinationCity":"Jackson, MI"},{"ident":"N1RJ","aircrafttype":"HDJT","filed_ete":"01:24:00","filed_time":1592481020,"filed_departuretime":1592479800,"filed_airspeed_kts":319,"filed_airspeed_mach":"","filed_altitude":350,"route":"PEGTE","actualdeparturetime":1592481126,"estimatedarrivaltime":1592486100,"actualarrivaltime":1592486100,"diverted":"","origin":"KJXN","destination":"KJQF","originName":"Jackson County","originCity":"Jackson, MI","destinationName":"Concord-Padgett Rgnl","destinationCity":"Concord, NC"}]}}
There very well may be a much more simple way of accomplishing what I need. Any example help is very much appreciated.
You can use Gson string to object mapper class directly as like below,
package com.sample.programs;
import java.util.List;
import com.google.gson.Gson;
public class FlightInfoResultMain {
public static void main(String[] args) {
String input = "{\"FlightInfoResult\":{\"next_offset\":-1,\"flights\":[{\"ident\":\"N1RJ\",\"aircrafttype\":\"HDJT\",\"filed_ete\":\"01:25:00\",\"filed_time\":1593038253,\"filed_departuretime\":1593037500,\"filed_airspeed_kts\":400,\"filed_airspeed_mach\":\"\",\"filed_altitude\":360,\"route\":\"WEAZL4 CLAWD\",\"actualdeparturetime\":1593038285,\"estimatedarrivaltime\":1593043320,\"actualarrivaltime\":1593043320,\"diverted\":\"\",\"origin\":\"KJQF\",\"destination\":\"KJXN\",\"originName\":\"Concord-Padgett Rgnl\",\"originCity\":\"Concord, NC\",\"destinationName\":\"Jackson County\",\"destinationCity\":\"Jackson, MI\"},{\"ident\":\"N1RJ\",\"aircrafttype\":\"HDJT\",\"filed_ete\":\"01:24:00\",\"filed_time\":1593000320,\"filed_departuretime\":1592998200,\"filed_airspeed_kts\":400,\"filed_airspeed_mach\":\"\",\"filed_altitude\":350,\"route\":\"PEGTE\",\"actualdeparturetime\":1593000420,\"estimatedarrivaltime\":1593005149,\"actualarrivaltime\":1593005149,\"diverted\":\"\",\"origin\":\"KJXN\",\"destination\":\"KJQF\",\"originName\":\"Jackson County\",\"originCity\":\"Jackson, MI\",\"destinationName\":\"Concord-Padgett Rgnl\",\"destinationCity\":\"Concord, NC\"},{\"ident\":\"N1RJ\",\"aircrafttype\":\"HDJT\",\"filed_ete\":\"01:29:00\",\"filed_time\":1592518049,\"filed_departuretime\":1592513400,\"filed_airspeed_kts\":317,\"filed_airspeed_mach\":\"\",\"filed_altitude\":360,\"route\":\"WEAZL4 CLAWD\",\"actualdeparturetime\":1592517936,\"estimatedarrivaltime\":1592523120,\"actualarrivaltime\":1592523120,\"diverted\":\"\",\"origin\":\"KJQF\",\"destination\":\"KJXN\",\"originName\":\"Concord-Padgett Rgnl\",\"originCity\":\"Concord, NC\",\"destinationName\":\"Jackson County\",\"destinationCity\":\"Jackson, MI\"},{\"ident\":\"N1RJ\",\"aircrafttype\":\"HDJT\",\"filed_ete\":\"01:24:00\",\"filed_time\":1592481020,\"filed_departuretime\":1592479800,\"filed_airspeed_kts\":319,\"filed_airspeed_mach\":\"\",\"filed_altitude\":350,\"route\":\"PEGTE\",\"actualdeparturetime\":1592481126,\"estimatedarrivaltime\":1592486100,\"actualarrivaltime\":1592486100,\"diverted\":\"\",\"origin\":\"KJXN\",\"destination\":\"KJQF\",\"originName\":\"Jackson County\",\"originCity\":\"Jackson, MI\",\"destinationName\":\"Concord-Padgett Rgnl\",\"destinationCity\":\"Concord, NC\"}]}}";
System.out.println("input - " + input);
//Create Gson object
Gson gson = new Gson();
FlightInfoResultObject responseObject = gson.fromJson(input, FlightInfoResultObject.class);
// parsing response to java pojo
List<Flights> listOfFlights = responseObject.getFlightInfoResult().getFlights();
for (Flights flight : listOfFlights) {
System.out.println("flight - " + flight.getIdent());
}
}
}
Object Mapping class: Inside Flights.class you can add all the variable which you have in json response.
class FlightInfoResultObject {
FlightInfoResult FlightInfoResult;
#Getter
#Setter
}
class FlightInfoResult {
Integer next_offset;
List<Flights> flights;
#Getter
#Setter
}
class Flights {
String ident;
String aircrafttype;
#Getter
#Setter
}
I think it's impossible to retrieve the inner JSON element directly using gson.
You have to get FlightInfoResult then flights accordingly. This should work:
Gson gson = new Gson();
JsonObject jsonObject = com.google.gson.JsonParser.parseString(responseString).getAsJsonObject();
JsonArray flightArray = jsonObject.getAsJsonObject("FlightInfoResult").getAsJsonArray("flights");
Type flightType = new TypeToken<List<Flight>>() {}.getType();
List<Flight> flights = gson.fromJson(flightArray, flightType);
Anyway, make sure the name of all properties inside class Flight matches Json elements name, to avoid mapping errors.
Since you didn't post your Flight class definition and gson naming rule, but the Flight class should be like:
public class Flight {
private String ident;
private String aircrafttype;
private String filed_ete;
private float filed_time;
private float filed_departuretime;
private float filed_airspeed_kts;
private String filed_airspeed_mach;
private float filed_altitude;
private String route;
private float actualdeparturetime;
private float estimatedarrivaltime;
private float actualarrivaltime;
private String diverted;
private String origin;
private String destination;
private String originName;
private String originCity;
private String destinationName;
private String destinationCity;
// getters setters
I have some json and it's fairly complex -- (a bit too complex and open-ended to model using something like gson), and I need to extract string values from certain nodes into a list of strings.
The following code works, but due to the way my json works -- it's grabbing lots of extra stuff that I don't want (note: I don't own the json schema)
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(json);
List<JsonNode> keys = node.findValues("key") ;
for(JsonNode key: keys){
System.out.println(key.toString());
}
The contents of Json is fairly complex (Jira filter export) which looks like this:
{
"issues": [
{
"key":"MIN-123",
...
"fields":{
"key":"A_Elric"
}
}
]
}
Assertions:
I always want to extract issues[x].key and not any of the subkeys. I would prefer to extract this into a list, but any normal data structure is fine. I'm already using Jackson -- but gson is also an option if there's a sane way of doing so.
Thanks for the assist!
JsonPath is xpath for json, and it has a Java implementation.
Here is a working example to get issue keys without subkeys:
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
public class JsonPathTest {
public static String ROOT_ARRAY = "issues";
public static String KEY = "key";
// get all KEYs right under ROOT array
public static String jsonPath = String.format("$.%s[*].%s", ROOT_ARRAY, KEY);
public static void main(String[] args) {
try {
String jsonStr = new String(Files.readAllBytes(Paths.get("c:/temp/xx.json")));
Object jsonObj = Configuration.defaultConfiguration().jsonProvider().parse(jsonStr);
List<String> keys = JsonPath.parse(jsonObj).read(jsonPath);
System.out.println(keys);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class ExportFilter{
private static final String KEY = "key";
private List<Map<String,Object>> issues = new ArrayList<>();
//getters and setters
#JsonIgnore
public List<String> getKeys(){
return issues.stream()
.map(issue-> issue.get(KEY))
.filter(Objects::nonNull)
.map(Objects::toString)
.collect(toList());
}
}
Example usage:
ObjectMapper objectMapper = new ObjectMapper();
List<String> keys = objectMapper.readValue( .., ExportFilter.class).getKeys();
I was using Jackson for all my json serialisation and deserialisation, but I'm trying to get my game working with GWT now so I'm moving over to the libgdx json parsing libraries.
Everything seems ok so far except this
HashMap<String, ArrayList<HighScoreEntry>> high_scores =
new HashMap<String, ArrayList<HighScoreEntry>>();
The ArrayList within the hashmap is being created as a array of JsonValue rather than an array of HighScoreEntry.
Can someone explain how I work around this? I know about json.setElementType(); but can't see how to use it in this instance. I'm playing with writing custom serialisation, but again, I can't work out how to extract exactly what I need.
I'm guessing in a custom serialiser I can use
json.readFields(this, jsonData);
to populate everything and then correct the erroneous data afterwards.
HighScoreEntry class (without methods):
public class HighScoreEntry implements Comparable<HighScoreEntry>, java.io.Serializable {
public long id;
public int score;
public String language = "en";
public String data;
public String name;
public boolean current;
}
Pointers would be appreciated.
I've worked something out, but I feel like there must be a better way. If anyone else has any ideas then please post them.
Adding a custom reader I can correct the corrupted high scores and convert them into instances of HighScoreEntry objects.
#Override
public void read(Json json, JsonValue jsonData) {
// Read all the fields
json.readFields(this, jsonData);
// replace high scores
HighScoreHashMapJsonValue screwedUpHashMap = json.readValue(HighScoreHashMapJsonValue.class, jsonData.get("high_scores"));
HashMap<String, Array<HighScoreEntry>> newHighScores = new HashMap<String, Array<HighScoreEntry>>();
for (Map.Entry<String, Array<JsonValue>> entry : screwedUpHashMap.entrySet()) {
String key = entry.getKey();
Array<JsonValue> jsonValueHighScores = entry.getValue();
Array<HighScoreEntry> highScoreArray = new Array<HighScoreEntry>();
newHighScores.put(key, highScoreArray);
for (JsonValue highScore : jsonValueHighScores) {
highScoreArray.add(json.readValue(HighScoreEntry.class, highScore));
}
}
high_scores = newHighScores;
}
public static class HighScoreHashMapJsonValue extends HashMap<String, Array<JsonValue>> {
}
I have class with some properties, for example:
public class MyClass {
public int number;
public String s;
}
and I want to convert Map of this class to json. for example:
Map<String, MyClass> map = new HashMap();
map.put("sss", new MyClass(1, "blabla");
json j = new json(map);
and I want the output to be like:
{"sss":{"number":"1","s":"blabla"}}
someone know how to do that in JAVA? I tried with JSONObject and with Gson but did not work for me.
you can use toJson() method of Gson class to convert a java object to json ,see the example below ,
public class SomeObject {
private int data1 = 100;
private String data2 = "hello";
private List<String> list = new ArrayList<String>() {
{
add("String 1");
add("String 2");
add("String 3");
}
};
//getter and setter methods
#Override
public String toString() {
return "SomeObject [data1=" + data1 + ", data2=" + data2 + ", list="
+ list + "]";
}
}
i will convert the above class' object to json , getter and setter methods are useful when you are converting the json back to java object .
public static void main(String[] args) {
SomeObject obj = new SomeObject();
Gson gson = new Gson();
// convert java object to JSON format,
// and returned as JSON formatted string
String json = gson.toJson(obj);
System.out.println(json);
}
output :
{"data1":100,"data2":"hello","list":["String 1","String 2","String 3"]}
Using Gson:
Gson gson = new GsonBuilder().create();
String json = gson.toJson(map);
You have to fix, parenthesis issue.
map.put("sss", new MyClass(1,"test")); //observe 2 braces at the end!
Following code should do the trick for you,
Gson gson = new Gson();
String myJson = gson.toJson(map);
Output:
{"sss":{"number":1,"s":"test"}}
Implement some custom toJSON() method for each class as shown below:
public class MyClass1 {
String number;
String name;
public MyClass1(String number, String name){
this.number = number;
this.name = name;
}
public JSONObject toJSON() throws JSONException {
return new JSONObject("{\"number\" : \""+this.number+"\", \"name\":\""+this.name+"\"}");
}
}
And then just use it to convert your map to jsonObject:
public class MapToJSON {
public static void main(String[] args) throws JSONException {
Map<String, JSONObject> map = new HashMap<String, JSONObject>();
map.put("sss", new MyClass1("1", "Hello").toJSON());
System.out.println(new JSONObject(map));
}
}
I found the way how to do that:
import com.google.gson.Gson;
import org.json.JSONObject;
Gson gson = new Gson();
map.put("sss", new JSONObject(gson.toJson(new MyClass(1, "Hello"))));
map.put("aaa", new JSONObject(gson.toJson(new MyClass(2, "blabla"))));
String output = new JSONObject(map).toString();
and now the output is correct.
Thanks a lot to all the people that tried to help me with this problem...
I am having a class like following,
public class Student {
public int id;
public String name;
public int age;
}
Now I want to create new Student,
//while create new student
Student stu = new Student();
stu.age = 25;
stu.name = "Guna";
System.out.println(new Gson().toJson(stu));
This gives me the following output,
{"id":0,"name":"Guna","age":25} //Here I want string without id, So this is wrong
So here I want String like
{"name":"Guna","age":25}
If I want to edit old Student
//While edit old student
Student stu2 = new Student();
stu2.id = 1002;
stu2.age = 25;
stu2.name = "Guna";
System.out.println(new Gson().toJson(stu2));
Now the output is
{"id":1002,"name":"Guna","age":25} //Here I want the String with Id, So this is correct
How can I make a JSON String with a field [At some point], without a field [at some point].
Any help will be highly appreciable.
Thanks.
Better is to use #expose annotation like
public class Student {
public int id;
#Expose
public String name;
#Expose
public int age;
}
And use below method to get Json string from your object
private String getJsonString(Student student) {
// Before converting to GSON check value of id
Gson gson = null;
if (student.id == 0) {
gson = new GsonBuilder()
.excludeFieldsWithoutExposeAnnotation()
.create();
} else {
gson = new Gson();
}
return gson.toJson(student);
}
It will ignore id column if that is set to 0, either it will return json string with id field.
You can explore the json tree with gson.
Try something like this :
gson.toJsonTree(stu1).getAsJsonObject().remove("id");
You can add some properties also :
gson.toJsonTree(stu2).getAsJsonObject().addProperty("id", "100");
JsonObject jsObj = (JsonObject) new Gson().toJsonTree(stu2);
jsObj.remove("age"); // remove field 'age'
jsObj.addProperty("key", "value"); // add field 'key'
System.out.println(jsObj);
You can manipulate with JsonObject
You should introduce additional field to Student class that will notice GSON about id serialization policy.
Then, you should implement custom serializer that will implement TypeAdapter. In your TypeAdapter implementation according to id serialization policy you will serialize it or not. Then you should register your TypeAdapter in GSON factory:
GsonBuilder gson = new GsonBuilder();
gson.registerTypeAdapter(Student.class, new StudentTypeAdapter());
Hope this helps.
You have two options.
Use Java's transient keyword which is to indicate that a field should not be serialized. Gson will exclude it automatically. This may not work for you as you want it conditionally.
Use #Expose annotation for the fields that you want and initialize your Gson builder as following:
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
So you need to mark name and age fields using #Expose and you need to have two different Gson instances for the default one which includes all fields and the one above which excludes fields without #Expose annotation.