Using Jackson to produce desired JSON response - java

Could anyone tell me what am I doing wrong while printing the JSON using Jackson. Here's my controller code :
#RequestMapping(value="/get_employee_details", method=RequestMethod.GET)
public String updateemployee
(
#RequestParam(value="emp_id", defaultValue="0") Integer emp_id,
#RequestParam(value="name", defaultValue="") String name
)
{
String responseJSON = null;
boolean getStatus = true;
try {
EmployeeDao employeeDao = (EmployeeDao)context.getBean("employeeDao");
Employee employee = null;
List<Employee> empList = employeeDao.findByEmployeeId(emp_id);
if ((empList != null) && (!empList.isEmpty())) {
List<String> empStatus = new ArrayList<String>();
for(Employee emp : empList){
empStatus.add(emp.addJoinString());
responseJSON = GenericOrmStatusView.OrmResponseToJsonString(true, 1,empStatus, true);
}
}
}
return responseJSON;
}
I have the following method defined in my Employee class :
public String addJoinString() {
return String.format("ID: %d",Name: %s," ,this.EmployeeId,this.name);
}
Since I am running a for loop in the code here and sending the list empStatus to the OrmResponseToJsonString method :
for(Employee emp : empList){
empStatus.add(emp.addJoinString());
responseJSON = GenericOrmStatusView.OrmResponseToJsonString(true, 1,empStatus, true);
I am getting the following JSON response :
{
"status" : "SUCCESS",
"employeeStatus" : [ "ID: 81, Name: Jack", "ID: 83, Name: Anthony", "ID: 88, Name: Stephanie", "ID: 25, Name: Kelly", "ID: 02, Name: Jessica" ]
}
However, I would like to be it in the following format:
{
"status" : "SUCCESS",
"message": " "
},
"employeeStatus":[{
"ID":81,
"Name":"Jack"
},
{
"ID":88,
"Name":"Anthony"
},
and so on and so forth ....
]
For Reference:
My OrmResponseToJsonString method is defined as follows inside GenericOrmStatusView class
public class GenericOrmStatusView extends Views
{
public static String OrmResponseToJsonString(boolean success, List<String> eStatus,boolean pretty)
{
PrintemployeeStatusIDAndStatus statusMsg = WebServiceUtils.printNameAndID(success, eStatus);
String genericOrmStatusJsonString = null;
try {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(SerializationFeature.INDENT_OUTPUT, pretty);
genericOrmStatusJsonString = objectMapper.writerWithView(Views.Normal.class).writeValueAsString(statusMsg);
//genericOrmStatusJsonString = objectMapper.writerWithView(Views.Normal.class).writeValueAsString(eStatus);
} catch(Exception e) {
e.printStackTrace();
}
return genericOrmStatusJsonString;
}
}
And my printNameAndID method is defined as follows inside WebServiceUtils class :
public class WebServiceUtils
{
public static PrintNameAndID printNameAndID(boolean success, List<String> eStatus)
{
PrintNameAndID statusMsgAndRegID = new PrintNameAndID();
if (success) {
statusMsgAndRegID.setStatus("SUCCESS");
statusMsgAndRegID.setemployeeStatus(eStatus);
} else {
statusMsgAndRegID.setStatus("ERROR");
//statusMsgAndRegID.setemployeeStatus("");
}
return statusMsgAndRegID;
}
}

The easiest way to get desired JSON output, is to create an object model representing the data, e.g.
public class MyJSON {
private MyStatus status;
private List<EmployeeStatus> employeeStatus = new ArrayList<>();
public MyJSON(String status, String message) {
this.status = new MyStatus(status, message);
}
public void addEmployeeStatus(int id, String name) {
this.employeeStatus.add(new EmployeeStatus(id, name));
}
public MyStatus getStatus() {
return this.status;
}
public List<EmployeeStatus> getEmployeeStatus() {
return this.employeeStatus;
}
}
public class MyStatus {
private String status;
private String message;
public MyStatus(String status, String message) {
this.status = status;
this.message = message;
}
public String getStatus() {
return this.status;
}
public String getMessage() {
return this.message;
}
}
public class EmployeeStatus {
private int id;
private String name;
public EmployeeStatus(int id, String name) {
this.id = id;
this.name = name;
}
#JsonProperty("ID")
public int getId() {
return this.id;
}
#JsonProperty("Name")
public String getName() {
return this.name;
}
}
You can then create JSON text like this:
MyJSON myJSON = new MyJSON("SUCCESS", " ");
myJSON.addEmployeeStatus(81, "Jack");
myJSON.addEmployeeStatus(88, "Anthony");
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
String json = objectMapper.writeValueAsString(myJSON);
System.out.println(json);
Output
{
"status" : {
"status" : "SUCCESS",
"message" : " "
},
"employeeStatus" : [ {
"ID" : 81,
"Name" : "Jack"
}, {
"ID" : 88,
"Name" : "Anthony"
} ]
}
As mentioned by chrylis in a comment:
As a note, you can normally let Spring do this automatically and just return empList from your controller method.
Which for the above code would mean something like this:
#GetMapping(path="/foo", produces="application/json")
public MyJSON foo() {
MyJSON myJSON = new MyJSON("SUCCESS", " ");
myJSON.addEmployeeStatus(81, "Jack");
myJSON.addEmployeeStatus(88, "Anthony");
return myJSON;
}

Related

Spring Convert enity manager getResultList results to JSON

I'm quite new to spring and I'm using spring entitymanager.createquery to make query and get the result from database.
So when make a http get this method executes to get all the patient name
public List<PatientModel> fetchallpatients (){
Query q = entityManager.createNativeQuery("select * from patient p ");
List <PatientModel> patientList=q.getResultList();
System.out.println(patientList);
return patientList;
}
it is returning
[[
1,
"patientname",
"patientphone",
"patientgender",
"patientinsurance",
"patientage",
"patientdiagnosis",
"patientaddreq",
"patientemr"
],
[
2,
"test",
"patientphone",
"patientgender",
"patientinsurance",
"patientage",
"patientdiagnosis",
"patientaddreq",
"patientemr"
],
[
3,
"react",
"12312",
"male",
"react",
"56",
"dsa",
"ads",
"das"
],
[
4,
"sign",
"asd",
"male",
"react",
"56",
"Diagnose 2",
"Ventilator",
"on"
],
[
5,
"good",
"3213",
"male",
"react",
"56",
"Diagnose 3",
"ICU",
"on"
] ]
Front end I'm using react and I need this data to be in JSON format.
I have a POJO class for corresponding table:
package io.login.model;
import java.io.Serializable;
public class PatientModel {
String patientname;
String patientage;
String patientgender;
String patientinsurance;
String patientphone;
String patientaddreq;
String patientemr;
public String getPatientname() {
return patientname;
}
public void setPatientname(String patientname) {
this.patientname = patientname;
}
public String getPatientage() {
return patientage;
}
public void setPatientage(String patientage) {
this.patientage = patientage;
}
public String getPatientgender() {
return patientgender;
}
public void setPatientgender(String patientgender) {
this.patientgender = patientgender;
}
public String getPatientinsurance() {
return patientinsurance;
}
public void setPatientinsurance(String patientinsurance) {
this.patientinsurance = patientinsurance;
}
public String getPatientphone() {
return patientphone;
}
public void setPatientphone(String patientphone) {
this.patientphone = patientphone;
}
public String getPatientdiagnosis() {
return patientdiagnosis;
}
public void setPatientdiagnosis(String patientdiagnosis) {
this.patientdiagnosis = patientdiagnosis;
}
public String getPatientaddreq() {
return patientaddreq;
}
public void setPatientaddreq(String patientaddreq) {
this.patientaddreq = patientaddreq;
}
public String getPatientemr() {
return patientemr;
}
public void setPatientemr(String patientemr) {
this.patientemr = patientemr;
}
String patientdiagnosis;
public PatientModel(String patientname, String patientage, String patientgender, String patientinsurance, String patientphone, String patientdiagnosis, String patientaddreq, String patientemr) {
this.patientname = patientname;
this.patientage = patientage;
this.patientgender = patientgender;
this.patientinsurance = patientinsurance;
this.patientphone = patientphone;
this.patientdiagnosis = patientdiagnosis;
this.patientaddreq = patientaddreq;
this.patientemr = patientemr;
}
}
But I need the result like
{ { "patientname":"xyz"
"patientphone":"xyz"
"patientinsurance":"xyz"
"patientgender":"xyz"
"patientdiagnosis":"xyz"
"patientaddreq":"xyz" ... so on
}
}
you are returning List<String[]> and expect it to be a JSON list of your POJO?
change your method to return what is expected, like:
#Transactional
public List<PatientModel> fetchallpatients () {
Query q = entityManager.createNativeQuery("select * from patient p ");
List <PatientModel> patientList = q.getResultList();
System.out.println(patientList);
return patientList;
}

how to return json objects for list<POJO> in RestController

I have restful webservice returning list of pojo objects which is not generating correct json format.
The following is the code which is not return json object
#RequestMapping("/getThisWeekPlan")
public List<ShiftPlannerView> getThisWeekPlan() {
return getShiftPlanRepo.fetchThisWeekShiftPlan();
}
My POJO where setting the result from JPA with hibernate using namedquery
public class ShiftPlannerView {
public ShiftPlannerView() {
}
private Date shiftPlannerDate;
private String resourceName;
private String shiftName;
public ShiftPlannerView(Date shiftPlannerDate, String resourceName, String shiftName) {
super();
this.shiftPlannerDate = shiftPlannerDate;
this.resourceName = resourceName;
this.shiftName = shiftName;
}
public Date getShiftPlannerDate() {
return shiftPlannerDate;
}
public void setShiftPlannerDate(Date shiftPlannerDate) {
this.shiftPlannerDate = shiftPlannerDate;
}
public String getResourceName() {
return resourceName;
}
public void setResourceName(String resourceName) {
this.resourceName = resourceName;
}
public String getShiftName() {
return shiftName;
}
public void setShiftName(String shiftName) {
this.shiftName = shiftName;
}
#Override
public String toString() {
return "ShiftPlannerView [shiftPlannerDate=" + shiftPlannerDate + ", resourceName=" + resourceName
+ ", shiftName=" + shiftName + "]";
}
}
DB call
#Repository
public class GetShiftPlanRepoImpl implements GetShiftPlanRepo{
#PersistenceContext
EntityManager em;
#Override
public List<ShiftPlannerView> fetchThisWeekShiftPlan() {
List<ShiftPlannerView> result= em.createNamedQuery("fetchByShiftPlannerThisWeek")
.getResultList();
return result;
}
}
The following is the response:
[
[
"2018-04-16",
"Elias",
"I"
],
[
"2018-04-16",
"Sithik",
"II"
],
[
"2018-04-17",
"Vikram Boya",
"I"
],
[
The following code changes brought me expected response
#Override
public List<ShiftPlannerView> fetchThisWeekShiftPlan() {
List<ShiftPlannerView> listOfShiftPlannerView=new ArrayList<>();
try {
Query q= em.createNativeQuery(ShiftPlannerConstants.THISWEEKSHIFTPLANQUERY);
List<Object[]> result=q.getResultList();
for(Object[] row : result){
ShiftPlannerView emp = new ShiftPlannerView();
Date workingDays = sdf.parse(row[0].toString());
emp.setShiftPlannerDate(workingDays);
emp.setResourceName(row[1].toString());
emp.setShiftName(row[2].toString());
listOfShiftPlannerView.add(emp);
}
}catch (Exception e) {
e.printStackTrace();
}
Thanks to JB!

Converting JSONArray to List<Object>?

I'm trying deserializes a JSONArray to List. To do it I'm trying use Gson but I can't understand why doesn't works and all values of JSON are null.
How could I do this ?
JSON
{ "result" : [
{ "Noticia" : {
"created" : "2015-08-20 19:58:49",
"descricao" : "tttttt",
"id" : "19",
"image" : null,
"titulo" : "ddddd",
"usuario" : "FERNANDO PAIVA"
} },
{ "Noticia" : {
"created" : "2015-08-20 19:59:57",
"descricao" : "hhhhhhhh",
"id" : "20",
"image" : "logo.png",
"titulo" : "TITULO DA NOTICIA",
"usuario" : "FERNANDO PAIVA"
} }
] }
Deserializes
List<Noticia> lista = new ArrayList<Noticia>();
Gson gson = new Gson();
JSONArray array = obj.getJSONArray("result");
Type listType = new TypeToken<List<Noticia>>() {}.getType();
lista = gson.fromJson(array.toString(), listType);
//testing - size = 2 but value Titulo is null
Log.i("LISTSIZE->", lista.size() +"");
for(Noticia n:lista){
Log.i("TITULO", n.getTitulo());
}
Class Noticia
public class Noticia implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String titulo;
private String descricao;
private String usuario;
private Date created;
private String image;
There are two problems with your code :
First is that you are using a getJsonArray() to get the array,
which isn't part of Gson library, you need to use
getAsJsonArray() method instead.
Second is that you are using array.toString() which isn't obvious
because for the fromJson method you need a jsonArray as
parameter and not String and that will cause you parse problems, just remove it.
And use the following code to convert your jsonArray to List<Noticia> :
Type type = new TypeToken<List<Noticia>>() {}.getType();
List<Noticia> lista = gson.fromJson(array, type);
And your whole code will be:
Gson gson = new Gson();
JSONArray array = obj.getAsJsonArray("result");
Type type = new TypeToken<List<Noticia>>() {}.getType();
List<Noticia> lista = gson.fromJson(array, type);
//testing - size = 2 but value Titulo is null
Log.i("LISTSIZE->", lista.size() +"");
for(Noticia n:lista){
Log.i("TITULO", n.getTitulo());
}
I think the problem could be something to do with toString() on JSONArray. But are you using obj.getAsJsonArray method?
Try this:
JSONArray arr = obj.getAsJsonArray("result");
Type listType = new TypeToken<List<Noticia>>() {
}.getType();
return new Gson().fromJson(arr , listType);
Noticia.java
public class Noticia {
private String created;
private String descricao;
private String id;
private String image;
private String titulo;
private String usuario;
public String getCreated() {
return created;
}
public void setCreated(String created) {
this.created = created;
}
public String getDescricao() {
return descricao;
}
public void setDescricao(String descricao) {
this.descricao = descricao;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getTitulo() {
return titulo;
}
public void setTitulo(String titulo) {
this.titulo = titulo;
}
public String getUsuario() {
return usuario;
}
public void setUsuario(String usuario) {
this.usuario = usuario;
}
#Override
public String toString() {
return "Noticia [created=" + created + ", descricao=" + descricao
+ ", id=" + id + ", image=" + image + ", titulo=" + titulo
+ ", usuario=" + usuario + "]";
}
}
Result.java
public class Result {
private Noticia Noticia;
public Noticia getNoticia() {
return Noticia;
}
public void setNoticia(Noticia noticia) {
Noticia = noticia;
}
#Override
public String toString() {
return "Result [Noticia=" + Noticia + "]";
}
}
Item.java
import java.util.List;
public class Item {
private List<Result> result;
public List<Result> getResult() {
return result;
}
public void setResult(List<Result> result) {
this.result = result;
}
#Override
public String toString() {
return "Item [result=" + result + "]";
}
}
Main.java
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.testgson.beans.Item;
public class Main {
private static Gson gson;
static {
gson = new GsonBuilder().create();
}
public static void main(String[] args) {
String j = "{\"result\":[{\"Noticia\":{\"created\":\"2015-08-20 19:58:49\",\"descricao\":\"tttttt\",\"id\":\"19\",\"image\":null,\"titulo\":\"ddddd\",\"usuario\":\"FERNANDO PAIVA\"}},{\"Noticia\":{\"created\":\"2015-08-20 19:59:57\",\"descricao\":\"hhhhhhhh\",\"id\":\"20\",\"image\":\"logo.png\",\"titulo\":\"TITULO DA NOTICIA\",\"usuario\":\"FERNANDO PAIVA\"}}]}";
Item r = gson.fromJson(j, Item.class);
System.out.println(r);
}
}
Final result
Item [result=[Result [Noticia=Noticia [created=2015-08-20 19:58:49, descricao=tttttt, id=19, image=null, titulo=ddddd, usuario=FERNANDO PAIVA]], Result [Noticia=Noticia [created=2015-08-20 19:59:57, descricao=hhhhhhhh, id=20, image=logo.png, titulo=TITULO DA NOTICIA, usuario=FERNANDO PAIVA]]]]
You parse json, that looks like
{ "result" : [
{
"created" : "2015-08-20 19:58:49",
"descricao" : "tttttt",
"id" : "19",
"image" : null,
"titulo" : "ddddd",
"usuario" : "FERNANDO PAIVA"
},
{
"created" : "2015-08-20 19:59:57",
"descricao" : "hhhhhhhh",
"id" : "20",
"image" : "logo.png",
"titulo" : "TITULO DA NOTICIA",
"usuario" : "FERNANDO PAIVA"
}
] }
You need to make another object Item and parse a list of them.
public class Item{
Noticia noticia;
}
Or you can interate through JSONArray, get field "noticia" from each then parse Noticia object from given JSONObject.
Kotlin Ex :
we getting response in form of JSONArry
call.enqueue(object : Callback<JsonArray> {
override fun onResponse(call: Call<JsonArray>, response: Response<JsonArray>) {
val list = response.body().toString()
val gson = Gson()
val obj: CitiesList? = gson.fromJson(list, CitiesList::class.java)
cityLiveData.value = obj!!
}
override fun onFailure(call: Call<JsonArray>, t: Throwable) {
}
})
Here CitiesList CitiesList::class.java is the ArrayList of Cities object
class CitiesList : ArrayList<CitiesListItem>()
Before using GSON add dependancey in Gradle
dependencies {
implementation 'com.google.code.gson:gson:2.8.8'
}

Unable to add array element under array using JSON

I have to generate JSON in below sample format:
[
{ "roleName" : "Parent Folder", "folderId" : "role1", "expanded" : true,
"children" :
[
{ "roleName" : "subUser1 non-openable folder", "folderId" : "role11","fileicon" : true },
{ "roleName" : "subUser2", "folderId" : "role12", "expanded" : true,
"children" :
[
{ "roleName" : "subUser2-1", "folderId" : "role121", "expanded" : true, "children" :
[
{ "roleName" : "subUser2-1-1 folder ico", "folderId" : "role1211" },
{ "roleName" : "subUser2-1-2 file ico", "folderId" : "role1212" , "fileicon" : true}
]
}
]
}
]
}
]
I have created POJO for same and was able to add array elements in, but unable to add one more array inside or below the element. Please suggest.
below are the pojo I am using.
public class TargetFolder
{
private TargetChildren[] children;
private String roleName;
private String expanded;
private Long folderId;
public TargetFolder(String roleName,
String isFolder, Long folderId, TargetChildren[] folderList) {
super();
this.roleName = roleName;
this.expanded = isFolder;
this.folderId = folderId;
this.children = folderList;
}
public TargetChildren[] getChildren ()
{
return children;
}
public void setChildren (TargetChildren[] children)
{
this.children = children;
}
public String getRoleName ()
{
return roleName;
}
public void setRoleName (String roleName)
{
this.roleName = roleName;
}
public String getExpanded ()
{
return expanded;
}
public void setExpanded (String expanded)
{
this.expanded = expanded;
}
public Long getFolderId ()
{
return folderId;
}
public void setFolderId (Long folderId)
{
this.folderId = folderId;
}
#Override
public String toString()
{
return "ClassPojo [children = "+children+", roleName = "+roleName+", expanded = "+expanded+", folderId = "+folderId+"]";
}
}
and
public class TargetChildren
{
private String fileicon;
private String roleName;
private long folderId;
public TargetChildren(String roleName, String fileicon, long folderId) {
super();
this.fileicon = fileicon;
this.roleName = roleName;
this.folderId = folderId;
}
public String getFileicon ()
{
return fileicon;
}
public void setFileicon (String fileicon)
{
this.fileicon = fileicon;
}
public String getRoleName ()
{
return roleName;
}
public void setRoleName (String roleName)
{
this.roleName = roleName;
}
public long getFolderId ()
{
return folderId;
}
public void setFolderId (long folderId)
{
this.folderId = folderId;
}
#Override
public String toString()
{
return "ClassPojo [fileicon = "+fileicon+", roleName = "+roleName+", folderId = "+folderId+"]";
}
}
and below is the logic I am using to generate the JSON:
for(int i = 0; i<folderList.size();i++)
{
if(folderList!=null)
{
subList = (List)folderList.get(i);
childFolders[i] = new TargetChildren((String)subList.get(0),(String)subList.get(2),(Long)subList.get(1));
JSONArray arr = new JSONArray();
if(((String)subList.get(2)).equals("true"))
{
arr.put(i, childFolders[i]);
}
System.out.println(arr.toString());
//TargetChildren [] testArr = new TargetChildren[] { new TargetChildren("Folder", "folderName", 226886843L)};
}
}
TargetFolder targetFolder = new TargetFolder(parentFoldername,isFolder,folderId, childFolders);
JSONObject jsonObject = new JSONObject(targetFolder);
String jsonString = jsonObject.toString();
System.out.println("JSON TO UI------ "+jsonString);
The most elegant and simple solution I can think of is adding a toJSON method to your POJOs and let it handle the serializing itself.
For the TargetFolder:
public JSONObject toJSON(){
JSONObject out = new JSONObject();
out.put("rolename", rolename);
out.put("expanded", expanded);
out.put("folderID", folderId);
JSONArray children = new JSONArray();
for(int i = 0; i < this.children.length; i++){
children.push(this.children[i].toJSON());
}
out.put("children", children);
return out;
}
Do the same for the TargetChildren and then you can convert it to JSON by calling:
myTargetFolder.toJSON();
This way you don't need to worry about the recursive structure of the resulting JSON.
If you add a constructor which takes a JSONObject, you can ensure consistent serialization and deserialization in one place.
There is also the GSON library from Google, which should achieve essentially the same, but I never used it, so I cannot say how it would work with that.
P.S.: You might want to create a common superclass for TargetFolder and TargetChild and use that as the datatype for the children-array, because from the JSON it seems like this array can contain objects with TargetFolder-properties ("expanded" and "children") and objects with TargetChild-properties ("fileicon")

Binding json, that has a list, with an object using Jackson

Given I have the following json:
{
"Company": {
"name": "cookieltd",
"type": "food",
"franchise_location": [
{
"location_type": "town",
"address_1": "5street"
},
{
"location_type": "village",
"address_1": "2road"
}
]
}
}
How can it be binded to the following object classes using Jackson?:
1) Company class
public class Company
{
String name, type;
List<Location> franchise_location = new ArrayList<Location>();
[getters and setters]
}
2) Location class
public class Location
{
String location_type, address_1;
[getters and setters]
}
I have done:
String content = [json above];
ObjectReader reader = mapper.reader(Company.class).withRootName("Company"); //read after the root name
Company company = reader.readValue(content);
but I am getting:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "franchise_location"
As far as I can tell, you are simply missing an appropriately named getter for the field franchise_location. It should be
public List<Location> getFranchise_location() {
return franchise_location;
}
(and the setter)
public void setFranchise_location(List<Location> franchise_location) {
this.franchise_location = franchise_location;
}
Alternatively, you can annotate your current getter or field with
#JsonProperty("franchise_location")
private List<Location> franchiseLocation = ...;
which helps to map JSON element names that don't really work with Java field name conventions.
The following works for me
public static void main(String[] args) throws Exception {
String json = "{ \"Company\": { \"name\": \"cookieltd\", \"type\": \"food\", \"franchise_location\": [ { \"location_type\": \"town\", \"address_1\": \"5street\" }, { \"location_type\": \"village\", \"address_1\": \"2road\" } ] } }";
ObjectMapper mapper = new ObjectMapper();
ObjectReader reader = mapper.reader(Company.class).withRootName(
"Company"); // read after the root name
Company company = reader.readValue(json);
System.out.println(company.getFranchise_location().get(0).getAddress_1());
}
public static class Company {
private String name;
private String type;
private List<Location> franchise_location = new ArrayList<Location>();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public List<Location> getFranchise_location() {
return franchise_location;
}
public void setFranchise_location(List<Location> franchise_location) {
this.franchise_location = franchise_location;
}
}
public static class Location {
private String location_type;
private String address_1;
public String getLocation_type() {
return location_type;
}
public void setLocation_type(String location_type) {
this.location_type = location_type;
}
public String getAddress_1() {
return address_1;
}
public void setAddress_1(String address_1) {
this.address_1 = address_1;
}
}
and prints
5street
my solution for JSON is always GSON, you can do some research on that, as long as you have the correct structure of class according to the JSON, it can automatically transfer from JSON to object:
Company company = gson.fromJson(json, Company.class);
GSON is so smart to do the convertion thing!
enjoy GSON !

Categories

Resources