I am getting the Json response from my symfony web app.
[
{
"Id": 15,
"trackingcode": "LVREF45771",
"Datecreation": {
"date": "2015-01-01 00:00:00.000000",
"timezone_type": 3,
"timezone": "Europe\/Berlin"
},
"Status": "Delivered",
"Livreur": {
"image": "d81aa404f6afd393ebf6e00bf87d4107.jpeg",
"idlivreur": 1,
"nom": "Aramex",
"prenom": "Aramex",
"adresse": "Aramex Adresse",
"numtel": 96250615,
"active": null,
"commandes": []
}
},
{
"Id": 16,
"trackingcode": null,
"Datecreation": {
"date": "2015-01-01 00:00:00.000000",
"timezone_type": 3,
"timezone": "Europe\/Berlin"
},
"Status": "Not delivered",
"Livreur": {
"image": "62d44c60a0ded39190552a7677a59bb3.jpeg",
"idlivreur": 2,
"nom": "FedEx",
"prenom": "Livraison",
"adresse": "FedEx adresse",
"numtel": 96250615,
"active": null,
"commandes": []
}
}
]
I am currently working on a mobile app using CodeNameOne. I've successfully parsed some data such as "Id" "trackingcode"
My problem is I didn't find a proper way to access "Livreur" fields ( image, idLivreur... )
Here is my code for parsing
JSONParser j = new JSONParser();
Map<String, Object> tasksListJson = j.parseJSON(new CharArrayReader(jsonText.toCharArray()));
List<Map<String, Object>> list = (List<Map<String, Object>>) tasksListJson.get("root");
for (Map<String, Object> obj : list) {
Livraison livraison = new Livraison();
float id = Float.parseFloat(obj.get("Id").toString());
livraison.setIdLivraison((int) id);
if (obj.get("trackingcode") == null) {
livraison.setTrackingCode("NO TRACKING CODE");
} else {
livraison.setTrackingCode(obj.get("trackingcode").toString());
}
livraison.setStatus(obj.get("Status").toString());
You should be able to access like this.
if (obj.get("Livreur") == null) {
Map<String, Object> data = (Map<String, Object>) obj.get("Livreur");
// now access inner fields like this.
data.get("image");
}
Or you can make use of beautiful Jackson
library. You can simply create a structure like this. (don't forget to add getter/setter)
class Livreur {
private String image;
// others
}
class WhateverNameYouWant {
private int id;
private String trackingcode;
// and others
private Livreur livreur;
//you can add similarly for date creation and others.
}
Now change your reader part.
ObjectMapper mapper = new ObjectMapper();
List<WhateverNameYouWant> list = mapper.readValue(payload, new TypeReference<List<WhateverNameYouWant>>(){});
As your fields seem to be mixed case, register case insensitive
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
And you can use the following to ignore unwanted fields.
@JsonIgnoreProperties(ignoreUnknown = true)
class WhateverNameYouWant { }
// similarly on other classes
I hope this helps.
You got 99% of the way:
for (Map<String, Object> obj : list) {
// ...
Map<String, Object> livreur = obj.get("Livreur");
String livreurImage = (String)liverur.get("image");
// ...
}
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.