Related
I want to change this json list to another format by putting the word "data" before the list and included in Parentheses like I the examples I put it down
the rest controller that I use
#CrossOrigin(origins = "http://localhost:8080")
#GetMapping("/users")
public List<User> getAllUsers() {
return userRepository.findAll();
}
the response is like that
[
{
"id": 1,
"firstName": "test",
"lastName": "test",
"email": "tt",
"createdAt": null,
"createdBy": "12",
"updatedAt": null,
"updatedBy": "12"
},
{
"id": 2,
"firstName": "test",
"lastName": "test",
"email": "tt",
"createdAt": null,
"createdBy": "12",
"updatedAt": null,
"updatedBy": "12"
}
]
I want to make it like that
{
"data": [
{
"id": 1,
"firstName": "test",
"lastName": "test",
"email": "tt",
"createdAt": null,
"createdBy": "12",
"updatedAt": null,
"updatedBy": "12"
},
{
"id": 2,
"firstName": "test",
"lastName": "test",
"email": "tt",
"createdAt": null,
"createdBy": "12",
"updatedAt": null,
"updatedBy": "12"
}
]
}
You can create another model class with `data property
public class UserData {
private List<User> data
// getters and setters
}
Then change the return type in the controller
#CrossOrigin(origins = "http://localhost:8080")
#GetMapping("/users")
public UserData getAllUsers() {
List<User> users = userRepository.findAll();
return new UserData(users);
}
Or if you don't want to create additional models, you can use Map<String, List<User>>
#CrossOrigin(origins = "http://localhost:8080")
#GetMapping("/users")
public Map<String, List<User>> getAllUsers() {
List<User> users = userRepository.findAll();
return Collections.singletonMap("data",users);
}
Could anyone help me with this issue please, I have a JSONObject which is highly nested to many levels and I want to replace certain keys with different values? Below is my JSONOBject:
{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Put_Location_Request",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Location_Reference",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "ID",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}]
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Location_Data",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Location_ID",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Effective_Date",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Location_Name",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Location_Usage_Reference",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "ID",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}]
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Location_Type_Reference",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "ID",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}]
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Location_Attribute_Reference",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "ID",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}]
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Superior_Location_Reference",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "ID",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}]
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Inactive",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Latitude",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Longitude",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Altitude",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Time_Profile_Reference",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "ID",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}]
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Locale_Reference",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "ID",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}]
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Display_Language_Reference",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "ID",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}]
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Time_Zone_Reference",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "ID",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}]
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Default_Currency_Reference",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "ID",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}]
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "External_Name",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Default_Job_Posting_Location_Reference",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "ID",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}]
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Trade_Name",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Worksite_ID_Code",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Global_Location_Number",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Location_Identifier",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Default_Ship-To_Location_Reference",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "ID",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}]
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Associate_Deliver-To_Location_Reference",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "ID",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}]
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Instructional_Site_Data",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Academic_Unit_Reference",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "ID",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}]
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Instructional_Site_Data_Capacity",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Off-Site",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Instructional_Use_Only",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}]
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Student_Housing_Assignment_Data",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Academic_Unit_Reference",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "ID",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}]
}]
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Location_Training_Room_Data",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Link",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Capacity",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Directions",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Equipment",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Reservation_Details",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Video_Conference_Details",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Additional_Details",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}]
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Contact_Data",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Address_Data",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Country_Reference",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "ID",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}]
}, .........
In the above JSONObject I want to replace key nullable with $nullable and localPart with $localPart etc. Can anyone help me with Java code.
I solved the problem with the following code:
import java.util.Iterator;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class ReplaceKeyInNestedJson {
public static void removeJSONField(JSONObject obj) throws JSONException {
obj.put("$nullable", obj.remove("nullable"));
obj.put("$localPart", obj.remove("localPart"));
obj.put("$prefix", obj.remove("prefix"));
obj.put("$precision", obj.remove("precision"));
obj.put("$name", obj.remove("name"));
obj.put("$namespace", obj.remove("namespace"));
obj.put("$scale", obj.remove("scale"));
Iterator<String> it = obj.keys();
while (it.hasNext()) {
String key = it.next();
Object childObj = obj.get(key);
if (childObj instanceof JSONArray) {
JSONArray arrayChildObjs = ((JSONArray) childObj);
int size = arrayChildObjs.length();
for (int i = 0; i < size; i++) {
removeJSONField(arrayChildObjs.getJSONObject(i));
}
}
if (childObj instanceof JSONObject) {
removeJSONField(((JSONObject) childObj));
}
}
}
public static void main(String[] args) {
String jsonStr = "{\n" + " \"nullable\": null,\n" + " \"localPart\": null,\n" + " \"prefix\": null,\n"
+ " \"precision\": null,\n" + " \"name\": \"Put_Location_Request\",\n" + " \"namespace\": null,\n"
+ " \"scale\": null,\n" + " \"unicode\": null,\n" + " \"type\": null,\n" + " \"maxLength\": null,\n"
+ " \"srid\": null,\n" + " \"subEntities\": [\n" + " {\n" + " \"nullable\": null,\n"
+ " \"localPart\": null,\n" + " \"prefix\": null,\n" + " \"precision\": null,\n"
+ " \"name\": \"Location_Reference\",\n" + " \"namespace\": null,\n"
+ " \"scale\": null,\n" + " \"unicode\": null,\n" + " \"type\": null,\n"
+ " \"maxLength\": null,\n" + " \"srid\": null,\n" + " \"subEntities\": [{\n"
+ " \"nullable\": null,\n" + " \"localPart\": null,\n" + " \"prefix\": null,\n"
+ " \"precision\": null,\n" + " \"name\": \"ID\",\n"
+ " \"namespace\": null,\n" + " \"scale\": null,\n" + " \"unicode\": null,\n"
+ " \"type\": null,\n" + " \"maxLength\": null,\n" + " \"srid\": null,\n"
+ " \"subEntities\": null\n" + " }]\n" + " }, {\n" + " \"nullable\": null,\n"
+ " \"localPart\": null,\n" + " \"prefix\": null,.............";
try {
JSONObject obj = new JSONObject(jsonStr);
removeJSONField(obj);
System.out.println(obj);
} catch (JSONException e) {
e.printStackTrace();
}
}}
If you are using faster.Jackson library you can solve like below:
import java.io.IOException;
import java.util.Iterator;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
public class ReplaceKeyInNestedJson2 {
private static final String FIELD_TYPE = "FieldType";
public static JsonNode removeJSONField(JsonNode obj) {
ObjectNode object = (ObjectNode) obj;
object.put("$kind", FIELD_TYPE);
object.set("$nullable", object.get("nullable"));
object.remove("nullable");
object.set("$localPart", object.get("localPart"));
object.remove("localPart");
object.set("$prefix", object.get("prefix"));
object.remove("prefix");
object.set("$precision", object.get("precision"));
object.remove("precision");
object.set("$name", object.get("name"));
object.remove("name");
object.set("$namespace", object.get("namespace"));
object.remove("namespace");
object.set("$scale", object.get("scale"));
object.remove("scale");
Iterator<String> it = obj.fieldNames();
while (it.hasNext()) {
String key = it.next();
JsonNode childObj = obj.get(key);
if (childObj.isArray()) {
ArrayNode arrayChildObjs = ((ArrayNode) childObj);
int size = arrayChildObjs.size();
for (int i = 0; i < size; i++) {
removeJSONField(arrayChildObjs.get(i));
}
}
if (childObj.isObject()) {
removeJSONField(((JsonNode) childObj));
}
}
return obj;
}
public static void main(String[] args) throws IOException {
try {
ObjectMapper mapper = new ObjectMapper();
JsonNode obj = mapper.readTree(StaticData.fieldsJson);
JsonNode result = removeJSONField(obj);
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
}
This question already has answers here:
Parse JSON array response using Retrofit & Gson
(4 answers)
Closed 4 years ago.
{
"name": "",
"link": "",
"menuId": "",
"icon": null,
"desc": null,
"selected": false,
"subMenus": [
{
"name": "Dashboard",
"link": "/welcome",
"menuId": "das",
"icon": "fa fa-tachometer fa-2x",
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Master",
"link": "",
"menuId": "emp",
"icon": "fa fa-user-plus fa-2x",
"desc": null,
"selected": false,
"subMenus": [
{
"name": "Roles",
"link": "/myRoles",
"menuId": "emp-usr-rol",
"icon": null,
"desc": null,
"selected": false,
"subMenus": [
{
"name": "Create Role",
"link": null,
"menuId": "emp-usr-rol-add",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Delete Role",
"link": null,
"menuId": "emp-usr-rol-del",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Edit Role",
"link": null,
"menuId": "emp-usr-rol-edt",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "List Role",
"link": null,
"menuId": "emp-usr-rol-lst",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
},
{
"name": "User",
"link": "/users",
"menuId": "emp-usr",
"icon": null,
"desc": null,
"selected": false,
"subMenus": [
{
"name": "Create User",
"link": null,
"menuId": "emp-usr-add",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Delete User",
"link": null,
"menuId": "emp-usr-del",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Edit User",
"link": null,
"menuId": "emp-usr-edt",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "List User",
"link": null,
"menuId": "emp-usr-lst",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
},
{
"name": "Departments",
"link": "/department",
"menuId": "emp-mst-dpt",
"icon": null,
"desc": null,
"selected": false,
"subMenus": [
{
"name": "Create Department",
"link": null,
"menuId": "emp-mst-dpt-add",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Delete Department",
"link": null,
"menuId": "emp-mst-dpt-del",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Edit Department",
"link": null,
"menuId": "emp-mst-dpt-edt",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "List Department",
"link": null,
"menuId": "emp-mst-dpt-lst",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
},
{
"name": "Salary Template",
"link": "",
"menuId": "emp-usr-rol",
"icon": null,
"desc": null,
"selected": false,
"subMenus": [
{
"name": "Create Salary Template",
"link": null,
"menuId": "emp-usr-rol-add",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Delete Salary Template",
"link": null,
"menuId": "emp-usr-rol-del",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Edit Salary Template",
"link": null,
"menuId": "emp-usr-rol-edt",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "List Salary Template",
"link": null,
"menuId": "emp-usr-rol-lst",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
},
{
"name": "Job Schedular",
"link": "",
"menuId": "emp-usr-rol",
"icon": null,
"desc": null,
"selected": false,
"subMenus": [
{
"name": "Create Job Schedular",
"link": null,
"menuId": "emp-usr-rol-add",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Delete Job Schedular",
"link": null,
"menuId": "emp-usr-rol-del",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Edit Job Schedular",
"link": null,
"menuId": "emp-usr-rol-edt",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "List Job Schedular",
"link": null,
"menuId": "emp-usr-rol-lst",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
},
{
"name": "Shift Management",
"link": "",
"menuId": "emp-usr-rol",
"icon": null,
"desc": null,
"selected": false,
"subMenus": [
{
"name": "Create Shift Management",
"link": null,
"menuId": "emp-usr-rol-add",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Delete Shift Management",
"link": null,
"menuId": "emp-usr-rol-del",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Edit Shift Management",
"link": null,
"menuId": "emp-usr-rol-edt",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "List Shift Management",
"link": null,
"menuId": "emp-usr-rol-lst",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
},
{
"name": "Leave Management",
"link": "/holidays",
"menuId": "hrm",
"icon": "fa fa-briefcase fa-2x",
"desc": null,
"selected": false,
"subMenus": [
{
"name": "Holidays",
"link": "/holidays",
"menuId": "emp-mst-hdy",
"icon": null,
"desc": null,
"selected": false,
"subMenus": [
{
"name": "Create Holiday",
"link": null,
"menuId": "emp-mst-hdy-add",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Delete Holiday",
"link": null,
"menuId": "emp-mst-hdy-del",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Edit Holiday",
"link": null,
"menuId": "emp-mst-hdy-edt",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "List Holiday",
"link": null,
"menuId": "emp-mst-hdy-lst",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
},
{
"name": "Leave Template",
"link": "",
"menuId": "emp-usr-rol",
"icon": null,
"desc": null,
"selected": false,
"subMenus": [
{
"name": "Create Leave Template",
"link": null,
"menuId": "emp-usr-rol-add",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Delete Leave Template",
"link": null,
"menuId": "emp-usr-rol-del",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Edit Leave Template",
"link": null,
"menuId": "emp-usr-rol-edt",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "List Leave Template",
"link": null,
"menuId": "emp-usr-rol-lst",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
}
]
}
]
},
{
"name": "Employee Management",
"link": "",
"menuId": "emng",
"icon": "fa fa-user fa-2x",
"desc": null,
"selected": false,
"subMenus": [
{
"name": "Employee",
"link": "/users",
"menuId": "emng-emp",
"icon": null,
"desc": null,
"selected": false,
"subMenus": [
{
"name": "Add Employees",
"link": "/addEmployee",
"menuId": "emng-emp-add",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Delete Employees",
"link": null,
"menuId": "emng-emp-del",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Edit Employees",
"link": null,
"menuId": "emng-emp-edt",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "List Employees",
"link": null,
"menuId": "emng-emp-lst",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
},
{
"name": "Leave Approval",
"link": "/leaveApproval",
"menuId": "emng-lea",
"icon": null,
"desc": null,
"selected": false,
"subMenus": [
{
"name": "List Leave Approval",
"link": null,
"menuId": "emng-lea-lst",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Edit Leave Approval",
"link": null,
"menuId": "emng-lea-edt",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
},
{
"name": "Monthly Attendance",
"link": "/monthlyAttendance",
"menuId": "emng-mta",
"icon": null,
"desc": null,
"selected": false,
"subMenus": [
{
"name": "List Monthly Attendance",
"link": null,
"menuId": "emng-mta-lst",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
},
{
"name": "Daily Attendance",
"link": "/dailyAttendance",
"menuId": "emng-dla",
"icon": null,
"desc": null,
"selected": false,
"subMenus": [
{
"name": "List Daily Attendance",
"link": null,
"menuId": "emng-dla-lst",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
},
{
"name": "In Out Register",
"link": "/viewInOutRegister",
"menuId": "emng-ior",
"icon": null,
"desc": null,
"selected": false,
"subMenus": [
{
"name": "List In Out Register",
"link": null,
"menuId": "emng-ior-lst",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
}
]
},
{
"name": "Payroll Management",
"link": "",
"menuId": "pmng",
"icon": "fa fa-paypal fa-2x",
"desc": null,
"selected": false,
"subMenus": [
{
"name": "Salary Structure Template",
"link": "/overtime",
"menuId": "pmng-ot",
"icon": null,
"desc": null,
"selected": false,
"subMenus": [
{
"name": "Add Salary Structure Template",
"link": null,
"menuId": "pmng-ot-add",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "List Salary Structure Template",
"link": null,
"menuId": "pmng-ot-lst",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
},
{
"name": "Association of template for Employee",
"link": "",
"menuId": "pmng-bnk",
"icon": null,
"desc": null,
"selected": false,
"subMenus": [
{
"name": " View template for Employee",
"link": null,
"menuId": "pmng-bnk-add",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "delete template for Employee",
"link": null,
"menuId": "pmng-bnk-del",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
},
{
"name": "Salary Slip",
"link": "",
"menuId": "pmng-ot",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
},
{
"name": "Employee Profile",
"link": "",
"menuId": "edsh",
"icon": "fa fa-users fa-2x",
"desc": null,
"selected": false,
"subMenus": [
{
"name": "Personal Details",
"link": "",
"menuId": "pmng-dtls",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Attendance",
"link": "",
"menuId": "edsh-emp",
"icon": null,
"desc": null,
"selected": false,
"subMenus": [
{
"name": "View Daily Attendance",
"link": null,
"menuId": "edsh-emp-add",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "View Monthly Attendance",
"link": null,
"menuId": "edsh-emp-lst",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
},
{
"name": "Salary Pacakge Structure",
"link": "",
"menuId": "pmng-bnk",
"icon": null,
"desc": null,
"selected": false,
"subMenus": [
{
"name": " View Salary Pacakge Structure ",
"link": null,
"menuId": "edsh-ler-add",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": " Pay Slip ",
"link": null,
"menuId": "edsh-ler-add",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
},
{
"name": "Leave Application",
"link": "/leaveRequest",
"menuId": "edsh-ler",
"icon": null,
"desc": null,
"selected": false,
"subMenus": [
{
"name": "Apply Leave ",
"link": null,
"menuId": "edsh-ler-add",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "View Leave Request",
"link": null,
"menuId": "edsh-ler-lst",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Edit Leave Request",
"link": null,
"menuId": "edsh-ler-edt",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
},
{
"name": "Shift Timings",
"link": "",
"menuId": "edsh-ior",
"icon": null,
"desc": null,
"selected": false,
"subMenus": [
{
"name": "View Shift Timings",
"link": null,
"menuId": "edsh-ior-lst",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
},
{
"name": "Holiday List",
"link": "/holidayList",
"menuId": "hld-lst",
"icon": null,
"desc": null,
"selected": false,
"subMenus": [
{
"name": "List Holiday",
"link": null,
"menuId": "emp-hld-lst",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
}
]
},
{
"name": "Package",
"link": "/pack",
"menuId": "set-pkg",
"icon": "fa fa-cogs fa-2x",
"desc": null,
"selected": true,
"subMenus": [
{
"name": "Create package",
"link": null,
"menuId": "set-pkg-add",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Delete package",
"link": null,
"menuId": "set-pkg-del",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Edit package",
"link": null,
"menuId": "set-pkg-edt",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "List package",
"link": null,
"menuId": "set-pkg-lst",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
},
{
"name": "Organization",
"link": "/org",
"menuId": "set-org",
"icon": "fa fa-cogs fa-2x",
"desc": null,
"selected": true,
"subMenus": [
{
"name": "Create organization",
"link": null,
"menuId": "set-org-add",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Delete organization",
"link": null,
"menuId": "set-org-del",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Edit organization",
"link": null,
"menuId": "set-org-edt",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "List organization",
"link": null,
"menuId": "set-org-lst",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
}
]
}
Use the below link or something similar to first create a Java object class to hold the data
http://www.jsonschema2pojo.org/
The use GsonConverterFactory with Retrofit to convert the object. Below link tells you how to add it
https://futurestud.io/tutorials/retrofit-2-adding-customizing-the-gson-converter
Create Pojo class and implementsSerializable. Use this class to get a response.
try this,
class pojo implements Serializable{
#SerializedName("name")
private String name;
public long getName() {
return name;
}
public void setId(String name) {
this.name = name;
}
}
enter image description here JSON Response:-
{
"hotelogix": {
"version": "1.0",
"datetime": "2017-01-17T11:37:58",
"response": {
"status": {
"code": 0,
"message": "success"
},
"nightAuditDate": "2015-04-15",
"lastUpdatedOn": "2017-01-17 11:05:40",
"isUpdated": true,
"totalRecords": "2",
"totalPages": 1,
"bookings": [
{
"isGroup": false,
"group": null,
"id": "gbighAuuug||",
"mainId": "gbighAuuug||",
"checkInDate": "2015-04-15",
"checkOutDate": "2015-04-16",
"adult": 2,
"child": 0,
"infant": 0,
"code": "12281852",
"reservationStatus": "RESERVE",
"businessSourcesId": null,
"source": "PMS",
"preference": "",
"roomStays": [
{
"date": "2015-04-15",
"roomTypeId": "gb_BTEs|",
"roomTypeName": "old age rooms",
"roomTypeCode": "CLSccc",
"roomId": "0",
"roomName": "",
"rateId": "gw||",
"rateName": "Seasonal Rate",
"amount": "600.000000",
"tax": "0.000000",
"discountAmount": "0.000000"
}
],
"guestStays": [
{
"id": "h2H4TkT2Bw||",
"mainId": "h2H4TkT2Bw||",
"checkInDate": "2015-04-15",
"checkOutDate": "2015-04-16",
"status": "RESERVE",
"isPrimary": true,
"isChargeSharer": true,
"type": "Adult",
"guestDetails": {
"id": "gsgPzJITYA||",
"code": "P949",
"salutation": null,
"fName": "Neha",
"lName": "Sharma",
"email": "neha#hotelogix.com",
"phoneNo": "55887799",
"mobileNo": "",
"gender": null,
"nationality": null,
"identityTypeId": null,
"identityNo": null,
"isBlackList": false,
"isVip": false,
"dob": "-0001-11-30",
"organization": null,
"designation": null,
"spouseSalutation": null,
"spouseFName": null,
"spouseLName": null,
"spouseDob": "0000-00-00 00:00:00",
"anniversary": "0000-00-00 00:00:00",
"addresses": {
"home": {
"address": "",
"country": null,
"state": null,
"city": "",
"zip": "",
"fax": null
},
"work": {
"address": "",
"country": null,
"state": null,
"city": "",
"zip": "",
"fax": null,
"phone": null,
"mobile": null
}
},
"files": []
}
},
{
"id": "h2H4TkT2Ag||",
"mainId": "h2H4TkT2Ag||",
"checkInDate": "2015-04-15",
"checkOutDate": "2015-04-16",
"status": "RESERVE",
"isPrimary": false,
"isChargeSharer": false,
"type": "Adult",
"guestDetails": {
"id": "gsgPzJITYg||",
"code": "P951",
"salutation": null,
"fName": "Mayajhanti",
"lName": "Jha",
"email": "mayanti#hotelogix.com",
"phoneNo": "01158988888",
"mobileNo": "99680480558",
"gender": "Male",
"nationality": "US",
"identityTypeId": null,
"identityNo": null,
"isBlackList": false,
"isVip": false,
"dob": "-0001-11-30",
"organization": null,
"designation": null,
"spouseSalutation": null,
"spouseFName": null,
"spouseLName": null,
"spouseDob": "0000-00-00 00:00:00",
"anniversary": "0000-00-00 00:00:00",
"addresses": {
"home": {
"address": "D-996, Cross Road",
"country": "US",
"state": "CA",
"city": "Los Angeles",
"zip": "325215",
"fax": null
},
"work": {
"address": "",
"country": null,
"state": null,
"city": "",
"zip": "",
"fax": null,
"phone": null,
"mobile": null
}
},
"files": [
{
"name": "Id Proof",
"url": "c://Users//mukesh//Desktop//abc.png"
},
{
"name": "Id Proof",
"url": "c://Users//mukesh//Desktop//abc.png"
}
]
}
}
],
"payments": [],
"otherCharges": [],
"addons": [],
"isHoldTill": false,
"releaseDate": "0000-00-00 00:00:00"
},
{
"isGroup": true,
"group": {
"id": "h2AIqHf1",
"mainId": "h2AIqHf1",
"checkInDate": "2015-04-15",
"checkOutDate": "2015-04-16",
"code": "G 0117433",
"groupStatus": "RESERVE",
"businessSourcesId": null,
"source": "",
"preference": "",
"ownerType": "Guest",
"owner": {
"id": "gsgPzf2HQw||",
"code": "P957",
"salutation": null,
"fName": "juna",
"lName": "mishra",
"email": "juna#hotelogix.com",
"phoneNo": "",
"mobileNo": "8802640811",
"gender": null,
"nationality": null,
"identityTypeId": null,
"identityNo": null,
"isBlackList": false,
"isVip": false,
"dob": "-0001-11-30",
"organization": null,
"designation": null,
"spouseSalutation": null,
"spouseFName": null,
"spouseLName": null,
"spouseDob": "0000-00-00 00:00:00",
"anniversary": "0000-00-00 00:00:00",
"addresses": {
"home": {
"address": "",
"country": null,
"state": null,
"city": "",
"zip": "",
"fax": null
},
"work": {
"address": "",
"country": null,
"state": null,
"city": "",
"zip": "",
"fax": null,
"phone": null,
"mobile": null
}
},
"files": []
},
"payTerm": 2,
"payments": [],
"otherCharges": [],
"groupLeader": []
},
"id": "gbihNuZBbA||",
"mainId": "gbihNuZBbA||",
"checkInDate": "2015-04-15",
"checkOutDate": "2015-04-16",
"adult": 1,
"child": 0,
"infant": 0,
"code": "01171859",
"reservationStatus": "RESERVE",
"businessSourcesId": null,
"source": "PMS",
"preference": "",
"roomStays": [
{
"date": "2015-04-15",
"roomTypeId": "gb_BTEs|",
"roomTypeName": "old age rooms",
"roomTypeCode": "CLSccc",
"roomId": "0",
"roomName": "",
"rateId": "gw||",
"rateName": "Seasonal Rate",
"amount": "500.000000",
"tax": "0.000000",
"discountAmount": "0.000000"
}
],
"guestStays": [
{
"id": "h2ANlxcGFg||",
"mainId": "h2ANlxcGFg||",
"checkInDate": "2015-04-15",
"checkOutDate": "2015-04-16",
"status": "RESERVE",
"isPrimary": true,
"isChargeSharer": true,
"type": "Adult",
"guestDetails": {
"id": "gsgPzf2HQg||",
"code": "P958",
"salutation": null,
"fName": "mina",
"lName": "sharma",
"email": "",
"phoneNo": "545487875454",
"mobileNo": "",
"gender": "Male",
"nationality": null,
"identityTypeId": null,
"identityNo": null,
"isBlackList": false,
"isVip": false,
"dob": "-0001-11-30",
"organization": null,
"designation": null,
"spouseSalutation": null,
"spouseFName": null,
"spouseLName": null,
"spouseDob": "0000-00-00 00:00:00",
"anniversary": "0000-00-00 00:00:00",
"addresses": {
"home": {
"address": "",
"country": null,
"state": null,
"city": "",
"zip": "",
"fax": null
},
"work": {
"address": "",
"country": null,
"state": null,
"city": "",
"zip": "",
"fax": null,
"phone": null,
"mobile": null
}
},
"files": []
}
}
],
"payments": [],
"otherCharges": [],
"addons": [],
"isHoldTill": false,
"releaseDate": "0000-00-00 00:00:00"
}
]
},
{
"xyx": {
"version": "1.0",
"datetime": "2016-12-13T05:27:08",
"response": {
"status": {
"code": 0,
"message": "success"
},
"hotels": [
{
"id": 6209,
"userTypes": [
{
"id": "UXjk0A||",
"title": "manager",
"status": "Active"
},
{
"id": "UJlhsA||",
"title": "test23",
"status": "Active"
},
{
"id": "UJll-Q||",
"title": "march",
"status": "Active"
},
{
"id": "UJlnNA||",
"title": "ajay mishra",
"status": "Active"
},
{
"id": "UJlnMw||",
"title": "prime",
"status": "Active"
},
{
"id": "UJlnPg||",
"title": "rr",
"status": "Active"
},
{
"id": "UJlnPw||",
"title": "xman",
"status": "Active"
},
{
"id": "UJloKg||",
"title": "sdd",
"status": "Active"
},
{
"id": "UJloKQ||",
"title": "tst1",
"status": "Active"
},
{
"id": "UJloLg||",
"title": "test2",
"status": "Active"
},
{
"id": "UJloLw||",
"title": "test3",
"status": "Active"
},
{
"id": "UJlpoA||",
"title": "kk",
"status": "Active"
},
{
"id": "UJlpqQ||",
"title": "shantanu manager",
"status": "Active"
},
{
"id": "UJhGcA||",
"title": "elbo",
"status": "Active"
}
]
}
]
},
"request": {
"method": "getusertypes",
"key": "02w7TK3e-0Ccoyo",
"data": {
"hotels": [
{
"id": 6209
}
]
}
}
}
}
My code:-
String getusertypestitlestring = jsonResult.getJSONObject("xyz").getJSONObject("response").getJSONArray("hotels").getJSONArray(0).getString(1);
:: i want to get userTypes>> title
:: i want to get bookings >> group >> code
Try this:
JSONArray userTypesArray = jsonResult.getJSONObject("xyx").getJSONObject("response").getJSONArray("hotels").getJSONObject(0).getJSONArray("userTypes");
for(int i =0 ; i< userTypesArray.length(); i++){
JSONObject userType = userTypesArray.getJSONObject(i);
System.out.println(userType.get("title"));
}
String getusertypestitlestring = jsonResult.getJSONObject("xyz").getJSONObject("response").getJSONArray("hotels").getJSONArray(0).getString(1);
Here it is not xyz, that is xyx
Make pojo of each Json Object in your Json String.
For Ex:
Make class POJO:
public class POJO {
XYX xyx;
public XYX getXyx() {
return xyx;
}
public void setXyx(XYX xyx) {
this.xyx = xyx;
}
}
Make class XYX:
public class XYX implements Serializable{
String version;
String datetime;
Response response;
Request request;
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getDatetime() {
return datetime;
}
public void setDatetime(String datetime) {
this.datetime = datetime;
}
public Response getResponse() {
return response;
}
public void setResponse(Response response) {
this.response = response;
}
public Request getRequest() {
return request;
}
public void setRequest(Request request) {
this.request = request;
}
}
Make class Response:
class Response {
Status status;
List<Hotels> hotels;
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
public List<Hotels> getHotels() {
return hotels;
}
public void setHotels(List<Hotels> hotels) {
this.hotels = hotels;
}
}
Make Class Request :
class Request {
String method;
String key;
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
}
and for List
Make class Hotels:
class Hotels {
String id;
List<UserTypes> userTypes;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public List<UserTypes> getUserTypes() {
return userTypes;
}
public void setUserTypes(List<UserTypes> userTypes) {
this.userTypes = userTypes;
}
}
and make all other pojo classes and use Gson library to set data in your pojos :
POJO pojo = new Gson().fromJson(jsonResponse,POJO.class);
And read Data from pojo :
System.out.println(pojo.getXyx().getDatetime());
System.out.println(pojo.getXyx().getRequest().getKey());
System.out.println(pojo.getXyx().getResponse().getHotels().get(0).getUserTypes().get(0).getTitle());
You can easily use GSON instead of JSON. It's more comfortable to work with the GSON objects. More input is available at:
https://google.github.io/gson/apidocs/com/google/gson/Gson.html
I have the following JSON string:
{
"_embedded": {
"issues": [
{
"projectId": 1,
"description": "description",
"reason": "reason",
"consequence": "consequence",
"category": null,
"severity": "HIGH",
"priority": "HIGH",
"source": "INTERN",
"owner": "owner",
"deadline": 1234567890,
"cost": 0,
"status": "OPEN",
"versionNo": 0,
"creationTimestamp": 1419255929860,
"lastUpdateTimestamp": 1419255929860,
"createdBy": "",
"lastUpdatedBy": "",
"_links": {
"self": {
"href": "http://lcalhost:8080/im-access/api/issues/1"
}
}
},
{
"projectId": 1,
"description": "description",
"reason": "reason",
"consequence": "consequence",
"category": null,
"severity": "HIGH",
"priority": "HIGH",
"source": "INTERN",
"owner": "owner",
"deadline": 1234567890,
"cost": 0,
"status": "OPEN",
"versionNo": 0,
"creationTimestamp": 1418911336913,
"lastUpdateTimestamp": 1418911336913,
"createdBy": "",
"lastUpdatedBy": "",
"_links": {
"self": {
"href": "http://lcalhost:8080/im-access/api/issues/2"
}
}
},
{
"projectId": 1,
"description": "description",
"reason": "reason",
"consequence": "consequence",
"category": null,
"severity": "HIGH",
"priority": "HIGH",
"source": "INTERN",
"owner": "owner",
"deadline": 1234567890,
"cost": 0,
"status": "OPEN",
"versionNo": 0,
"creationTimestamp": 1418911337383,
"lastUpdateTimestamp": 1418911337383,
"createdBy": "",
"lastUpdatedBy": "",
"_links": {
"self": {
"href": "http://lcalhost:8080/im-access/api/issues/3"
}
}
}
]
}
}
When trying to fetch this data, I use the following
RestTemplate restTemplate = new RestTemplate();
final IssueDTO[] responseEntity = restTemplate.getForObject("http://localhost:8080/im-access/api/issues", Embedded.class).getIssues();
and my Embedded class is this
#JsonIgnoreProperties(ignoreUnknown = true)
public class Embedded {
private IssueDTO[] issues;
public IssueDTO[] getIssues() {
return issues;
}
public void setIssues(IssueDTO[] issues) {
this.issues = issues;
}
}
However,I get a null pointer exception when trying to get the data.
It looks like your DTO structure is little off. When I generated your DTOs from http://www.jsonschema2pojo.org/ then I get a structure like below. I think you are missing one level of nesting in your DTO structure, thats why your deserializing is failing. So your code should like this,
final List<Issues> issues = restTemplate.getForObject(url,WrapperEmbedded.class).getEmbedded().getIssues();
Here is the trimmed version of DTOs, you can try yourself on http://www.jsonschema2pojo.org/ (make sure to select JSON as source type)
public class WrapperEmbedded {
#JsonProperty("_embedded")
private com.example.Embedded Embedded;
#JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object> ();
//GETTERS AND SETTERS
}
#JsonInclude(JsonInclude.Include.NON_NULL)
#Generated("org.jsonschema2pojo")
#JsonPropertyOrder({
"issues"
})
public class Embedded {
#JsonProperty("issues")
private List<Issue> issues = new ArrayList<Issue>();
//GETTERS AND SETTERS
}
#JsonInclude(JsonInclude.Include.NON_NULL)
#Generated("org.jsonschema2pojo")
#JsonPropertyOrder({
"_embedded"
})
#JsonInclude(JsonInclude.Include.NON_NULL)
#Generated("org.jsonschema2pojo")
#JsonPropertyOrder({
"projectId",
"description",
"reason",
"consequence",
"category",
"severity",
"priority",
"source",
"owner",
"deadline",
"cost",
"status",
"versionNo",
"creationTimestamp",
"lastUpdateTimestamp",
"createdBy",
"lastUpdatedBy",
"_links"
})
public class Issue {
//GETTERS AND SETTERS
}