I know that conversion of JSON string to JAVA object using Jackson object mapper package.
Suppose if I have advanced Json string like what I mentioned below. Is it possible to convert that as a Java object?
{
"a": 123123,
"b":true,
"cList":[{
"c1": "valuec1",
"c2": "valuec2",
"c3": "valuec3"
},
{
"c1": "valuec4",
"c2": "valuec5",
"c3": "valuec6"
}]
}
public class Codebeautify {
private float a;
private boolean b;
ArrayList < Object > cList = new ArrayList < Object > ();
// Getter Methods
public float getA() {
return a;
}
public boolean getB() {
return b;
}
// Setter Methods
public void setA(float a) {
this.a = a;
}
public void setB(boolean b) {
this.b = b;
}
}
class Example {
public int a;
public boolean b;
public List<Exmaple1> cList;
#Override
public String toString() {
return "Example [a=" + a + ", flag=" + b + ", e1=" + cList + "]";
}
}
class Exmaple1 {
public String c1;
public String c2;
public String c3;
public String getC1() {
return c1;
}
public void setC1(String c1) {
this.c1 = c1;
}
public String getC2() {
return c2;
}
public void setC2(String c2) {
this.c2 = c2;
}
public String getC3() {
return c3;
}
public void setC3(String c3) {
this.c3 = c3;
}
}
class B {
public String c1;
public String c2;
public String c3;
public B() {}
public B(String c1, String c2, String c3) {
this.c1 = c1;
this.c2 = c2;
this.c3 = c3;
}
#Override
public String toString() {
return "{" +
" c1 :'" + c1 + '\'' +
", c2 :'" + c2 + '\'' +
", c3 :'" + c3 + '\'' +
'}';
}
public String getC1() {return c1;}
public void setC1(String c1) {this.c1 = c1;}
public String getC2() {return c2;}
public void setC2(String c2) {this.c2 = c2;}
public String getC3() {return c3;}
public void setC3(String c3) {this.c3 = c3;}
}
public class C {
public int a;
public boolean b;
public List<B> cList;
public C() {}
public C(int a, boolean b, List<B> cList) {
this.a = a;
this.b = b;
this.cList = cList;
}
#Override
public String toString() {
return "{" +
" a :" + a +
", b :" + b +
", cList :" + cList +
'}';
}
public int getA() {return a;}
public void setA(int a) {this.a = a;}
public boolean isB() {return b;}
public void setB(boolean b) {this.b = b;}
public List<B> getcList() {return cList;}
public void setcList(List<B> cList) {this.cList = cList;}
public static void main(String[] args) {
List<B> cList = new ArrayList<>();
cList.add(new B("x1", "x2", "x3"));
cList.add(new B("y1", "y2", "y3"));
C obj = new C(123, true, cList);
ObjectMapper objectMapper = new ObjectMapper();
// obj --> json
String json = null;
try {
json = objectMapper.writeValueAsString(obj);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
System.out.println(json == null ? "error" : json);
// json1 --> obj1
//String json1 = "{\"a\":123,\"b\":true,\"cList\":[{\"c1\":\"x1\",\"c2\":\"x2\",\"c3\":\"x3\"},{\"c1\":\"y1\",\"c2\":\"y2\",\"c3\":\"y3\"}]}";
String json1 = json;
C obj1 = null;
try {
obj1 = objectMapper.readValue(json1, C.class);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(obj1 == null ? "error" : obj1);
}
}
Related
These are my classes.
public class Prova {
private static HashMap<String,UtenteAstratto> map = new HashMap<String,UtenteAstratto>();
public static void main(String[] args) {
DatiPersonali dp1 = new DatiPersonali("a", "a", "a", "a", "a", "a", "a", "a");
UtenteRegistrato u1 = new UtenteRegistrato(dp1);
map.put(u1.getUsername(), u1);
DatiPersonali dp2 = new DatiPersonali("b", "b", "b", "b", "b", "b", "b", "b");
UtenteRegistrato u2 = new UtenteRegistrato(dp2);
AdminDecorator ad = new AdminDecorator(u2);
map.put(ad.getUsername(), ad);
DatiPersonali dp3 = new DatiPersonali("c", "c", "c", "c", "c", "c", "c", "c");
UtenteRegistrato u3 = new UtenteRegistrato(dp3);
GestoreDecorator gd = new GestoreDecorator(u3);
map.put(gd.getUsername(), gd);
System.out.println(map.toString());
System.out.println();
save(map);
load();
}
private static void load() {
try {
String nomeFile = "fileProva.sav";
FileInputStream fis = new FileInputStream(nomeFile);
ObjectInputStream ois = new ObjectInputStream( fis );
Object o = ois.readObject();
if( !o.equals("") ) {
map = (HashMap<String,UtenteAstratto>) o;
for(Entry elem: map.entrySet()) {
System.out.println("username= " + elem.getKey() + " " + elem.getValue());
}
}
ois.close();
fis.close();
}catch( Exception e ) {
e.printStackTrace();
}
}
public static void save(Object o) {
try {
FileOutputStream fos = new FileOutputStream("fileProva.sav");
ObjectOutputStream oos = new ObjectOutputStream( fos );
oos.writeObject(o);
oos.close();
fos.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}
With this error.
java.lang.NullPointerException
at Utilities_Utente.UtenteDecorator.toString(UtenteDecorator.java:9)
at java.base/java.lang.String.valueOf(String.java:3352)
at java.base/java.lang.StringBuilder.append(StringBuilder.java:166)
at Utilities_Utente.Prova.load(Prova.java:43)
at Utilities_Utente.Prova.main(Prova.java:31)
Class GestoreDecorator
public class GestoreDecorator extends UtenteDecorator implements Serializable{
private static final long serialVersionUID = 8246098147192933576L;
public GestoreDecorator (UtenteAstratto u){
DatiPersonali dp =new DatiPersonali(u.getDatiPersonali());
utente = new UtenteRegistrato(dp);
utente.setPermesso(Permesso.GESTORE);
}
public void setPermesso(){
}
}
Class AdminDecorator
public class AdminDecorator extends UtenteDecorator implements Serializable{
private static final long serialVersionUID = -8816003037658470920L;
public AdminDecorator (UtenteAstratto u){
DatiPersonali dp = new DatiPersonali(u.getDatiPersonali());
utente = new UtenteRegistrato(dp);
// utente = (UtenteRegistrato) u;
utente.setPermesso(Permesso.ADMIN);
}
#Override
public void setPermesso() {
// TODO Auto-generated method stub
}
}
Class UtenteAstratto
public abstract class UtenteAstratto implements Comparable<UtenteAstratto>{
public enum Permesso { UTENTE, ADMIN, GESTORE };
public abstract String getUsername();//questo metodo serve nella classe Squadra.Squadra
public abstract String getPassword();//questo metodo serve nella classe Squadra.Squadra
public abstract DatiPersonali getDatiPersonali();//questo metodo serve nella classe Squadra.Squadra
public abstract Permesso getPermesso();
}
Class UtenteDecorator
public abstract class UtenteDecorator extends UtenteAstratto {
protected UtenteRegistrato utente;
public abstract void setPermesso();
public String toString(){
return utente.toString();
}
public int compareTo(UtenteAstratto o) {
return utente.compareTo(o);
}
public String getUsername() {
return utente.getUsername();
}
public String getPassword() {
return utente.getPassword();
}
public DatiPersonali getDatiPersonali() {
return utente.getDatiPersonali();
}
public Permesso getPermesso(){
return utente.getPermesso();
}
}
Class UtenteRegistrato
public class UtenteRegistrato extends UtenteAstratto implements Serializable{
private static final long serialVersionUID = -2593162236417203422L;
private DatiPersonali dp;
private Permesso permesso;
public UtenteRegistrato (DatiPersonali d) {
this.dp = d;
permesso = Permesso.UTENTE;
}//Costruttore
public Permesso getPermesso(){
return permesso;
//return
}
public void setPermesso(Permesso p) {
permesso = p;
}
public DatiPersonali getDatiPersonali (){
return dp;
}
public int hashCode() {
int parziale = super.hashCode();
final int primo = 41;
int result = parziale + primo * dp.hashCode() ;
return result;
}
public boolean equals(Object o) {
if (!(o instanceof UtenteRegistrato))
return false;
if (this == o)
return true;
UtenteRegistrato user = (UtenteRegistrato) o;
if (!getUsername().equals(user.getUsername()))
return false;
return true;
}//equals
public String toString() {
StringBuilder sb = new StringBuilder (500);
sb.append(permesso.toString() + " ");
sb.append(getDatiPersonali().toString());
return sb.toString();
}
public int compareTo(UtenteAstratto o) {
UtenteAstratto u = null;
if (o instanceof UtenteDecorator)
u= (UtenteDecorator) o;
else
u= (UtenteRegistrato) o;
if (getUsername().compareTo(u.getUsername())==0)
return 0;
if (dp.getCognome().compareTo(u.getDatiPersonali().getCognome()) <0)
return -1;
if (dp.getCognome().compareTo(u.getDatiPersonali().getCognome()) ==0 && dp.getNome().compareTo(u.getDatiPersonali().getNome()) <0)
return -1;
return 1;
}
#Override
public String getUsername() {
return dp.getUsername();
}
#Override
public String getPassword() {
return dp.getPassword();
}
}
And the views of debugger.
The view save.
enter image description here
The view load.
enter image description here
Now, my question is: Why when I load the Hashmap the value 1 and 2 (AdminDecorator and GestoreDecorator) are null?
You should read What is a NullPointerException, and how do I fix it?.
In particular, the stack trace is telling you which line of code is the problem:
java.lang.NullPointerException
at Utilities_Utente.UtenteDecorator.toString(UtenteDecorator.java:9)
The error occurred in UtenteDecorator.toString at line 9.
There is only one line of code in that toString method:
return utente.toString();
The only possible cause of a NullPointerException in that line of code is that utente is null.
The best thing you can do is force that field to never be null, by adding a constructor:
protected UtenteDecorator(UtenteRegistrato utente) {
this.utente = Objects.requireNonNull(utente,
"utente cannot be null.");
}
(Objects is the java.util.Objects class.)
If you don’t want to change the constructor, an alternative is to defensively code your toString method, so it won’t break when utente is null:
public String toString() {
return Objects.toString(utente, "(utente not defined)");
}
I have a CSV conversion class. It's job is to muck with values and produce few outputs. The CSV row has around 30 columns. I've modelled via immutable objects:
In(i1, i2, ..., iM)
OutA(a1, a2, ..., aN)
OutB(b1, b2, ..., bO)
...
OutK(k1, k2, ..., kP)
OutX(x1, x2, ..., xQ)
There are inter-dependencies: b1 is the same as a1, b2 is calculated based on a2, they combine to the final result (OutX). Some calculations are expensive.
The end result is a huge method that looks similar to this simplified monster:
OutX method(In in) {
I1 i1 = in.getI1();
I1 i2 = in.getI2();
...
I1 iM = in.getIM();
A1 a1 = fa1(i1, i2);
A2 a2 = fa2(i2, i5, iM);
...
AN aN = ...;
OutA outA = new OutA(
a1,
a2,
...,
aN);
A1 b1 = a1;
B2 b2 = fb2(a2, i5, i13);
...
BO bO = ...;
OutB outB = new OutB(
b1,
b2,
...,
bO);
...
return new OutX(
outA,
outB,
...,
outK);
}
'Tis wonderful being immutable and type-checked and stuff. 'Tis also 300 lines and this is for each "flavor" of CSV. Ugh. Breaking up just creates methods that are mostly parameters + constructor calls.
Are there patterns or libraries to un-Frankenstein this?
The Adapter pattern might be helpful. Each layer/shell delegates as much as possible to build up to the final result. Here's an example:
public class App {
public static void main(String[] args) {
App app = new App();
app.process();
}
private void process() {
In in = new In(1, 2, 3);
OutA a = new OutA(in);
OutB b = new OutB(a);
System.out.println(b);
}
public class In {
private final int i1;
private final int i2;
private final int i3;
public In(int i1, int i2, int i3) {
this.i1 = i1;
this.i2 = i2;
this.i3 = i3;
}
public int getI1() {
return i1;
}
public int getI2() {
return i2;
}
public int getI3() {
return i3;
}
}
public class OutA {
private final In in;
private Integer a3;
public OutA(In in) {
this.in = in;
}
public int getA1() {
return in.getI1();
}
public int getA2() {
return in.getI2() * 2;
}
public int getA3() {
if (a3 == null) {
// a3 = some expensive calculation
a3 = 1; // hold the value to avoid expensive calculation next time method is called
}
return a3;
}
}
public class OutB {
private final OutA a;
public OutB(OutA a) {
this.a = a;
}
public int getB1() {
return a.getA1();
}
public int getB2() {
// a.getA3() is expensive, but only the first time it's called
return a.getA3() + 4;
}
public int getB3() {
// a.getA3() is expensive, but only the first time it's called
return a.getA3() + 9;
}
public String toString() {
return "b1: " + getB1() + " b2: " + getB2() + " b3: " + getB3();
}
}
}
Output: b1: 1 b2: 5 b3: 10
UPDATE: here's a more interesting example using a factory to have different calculations, but still using the same interface.
public class App {
public static void main(String[] args) {
App app = new App();
app.process();
}
private void process() {
In in = new In(1, 2, 3);
OutXFactory factory = new OutXFactory();
OutX resultType1 = factory.create(in, CSVType.TYPE_1);
System.out.println(resultType1);
OutX resultType2 = factory.create(in, CSVType.TYPE_2);
System.out.println(resultType2);
}
public class In {
private final int i1;
private final int i2;
private final int i3;
public In(int i1, int i2, int i3) {
this.i1 = i1;
this.i2 = i2;
this.i3 = i3;
}
public int getI1() {
return i1;
}
public int getI2() {
return i2;
}
public int getI3() {
return i3;
}
}
public enum CSVType {
TYPE_1, TYPE_2;
}
public class OutA {
private final In in;
private Integer a3;
public OutA(In in) {
this.in = in;
}
public int getA1() {
return in.getI1();
}
public int getA2() {
return in.getI2() * 2;
}
public int getA3() {
if (a3 == null) {
// a3 = some expensive calculation
a3 = 1; // hold the value to avoid expensive calculation next time method is called
}
return a3;
}
}
public class OutB {
private final OutA a;
public OutB(OutA a) {
this.a = a;
}
public int getB1() {
return a.getA1();
}
public int getB2() {
// a.getA3() is expensive, but only the first time it's called
return a.getA3() + 4;
}
public int getB3() {
// a.getA3() is expensive, but only the first time it's called
return a.getA3() + 9;
}
}
public interface OutX {
public int getX1();
public int getX2();
public int getX3();
}
public abstract class AbstractOutX implements OutX {
#Override
public String toString() {
return "x1: " + getX1() + " x2: " + getX2() + " x3: " + getX3();
}
}
public class OutXA extends AbstractOutX {
private final OutA a;
public OutXA(OutA a) {
this.a = a;
}
#Override
public int getX1() {
return a.getA1() + 1;
}
#Override
public int getX2() {
return a.getA2() + a.getA3() + 2;
}
#Override
public int getX3() {
return a.getA1() + a.getA2() + 3;
}
}
public class OutXAB extends AbstractOutX {
private final OutA a;
private final OutB b;
public OutXAB(OutA a, OutB b) {
this.a = a;
this.b = b;
}
#Override
public int getX1() {
return a.getA1() + b.getB1();
}
#Override
public int getX2() {
return a.getA2() * b.getB2();
}
#Override
public int getX3() {
return (int) Math.pow(a.getA3(), b.getB3());
}
}
public class OutXFactory {
public OutX create(In in, CSVType type) {
if (type == CSVType.TYPE_1) {
OutA a = new OutA(in);
return new OutXA(a);
} else {
OutA a = new OutA(in);
OutB b = new OutB(a);
return new OutXAB(a, b);
}
}
}
}
I am convert a json data into POJO with array of objects using Codehaus Jackson.
I am having problems with the array of objects. I am getting errors like "Can not deserialize instance ... out of START_ARRAY token".
Below are my codes
JSON Data (flightItineraryPrice.json)
{
"tripType": "OneWay",
"tripInfos":[
{
"from":"EARTH",
"to":"MOON",
"fromSchedule":"2015-12-21T04:30:00",
"toSchedule":"2015-12-21T06:50:00"
},
{
"from":"MOON",
"to":"MARS",
"fromSchedule":"2015-12-21T03:30:00",
"toSchedule":"2015-12-21T011:10:00"
},
{
"from":"VENUS",
"to":"KEPLER",
"fromSchedule":"2015-12-21T01:30:00",
"toSchedule":"2015-12-21T22:30:00"
},
{
"from":"EARTH",
"to":"SUN",
"fromSchedule":"2015-12-20T02:30:00",
"toSchedule":"2015-12-29T15:10:00"
}
],
"adultFare":{
"paxType":"ADT",
"baseFare":"1000",
"totalFeesAndTaxes":"300",
"totalAmount":"1300.00"
},
"childFare":{
"paxType":"CHD",
"baseFare":"750",
"totalFeesAndTaxes":"250",
"totalAmount":"1000.00"
},
"infantFare":{
"paxType":"INF",
"baseFare":"250",
"totalFeesAndTaxes":"25",
"totalAmount":"275.00"
},
"adultCount":"1",
"childCount":"1",
"infantCount":"2"
}
Class (JacksonFlightItineraryPrice.java)
package com.jgtt.samples;
import java.util.*;
import org.codehaus.jackson.annotate.JsonProperty;
public class JacksonFlightItineraryPrice {
private String tripType;
#JsonProperty("tripInfos")
private JacksonFlightItineraryPrice.TripInfo tripInfos;
private JacksonFlightItineraryPrice.PaxFare adultFare;
private JacksonFlightItineraryPrice.PaxFare childFare;
private JacksonFlightItineraryPrice.PaxFare infantFare;
private short adultCount;
private short childCount;
private short infantCount;
public JacksonFlightItineraryPrice() {}
public String getTripType() {
return (this.tripType);
}
public void setTripType(String tripType) {
this.tripType = tripType;
}
public JacksonFlightItineraryPrice.TripInfo getTripInfos() {
return (this.tripInfos);
}
public void setTripInfos(JacksonFlightItineraryPrice.TripInfo tripInfos) {
this.tripInfos = tripInfos;
}
public JacksonFlightItineraryPrice.PaxFare getAdultFare() {
return (this.adultFare);
}
public void setAdultFare(JacksonFlightItineraryPrice.PaxFare adultFare) {
this.adultFare = adultFare;
}
public JacksonFlightItineraryPrice.PaxFare getChildFare() {
return (this.childFare);
}
public void setChildFare(JacksonFlightItineraryPrice.PaxFare childFare) {
this.childFare = childFare;
}
public JacksonFlightItineraryPrice.PaxFare getInfantFare() {
return (this.infantFare);
}
public void setInfantFare(JacksonFlightItineraryPrice.PaxFare infantFare) {
this.infantFare = infantFare;
}
public short getAdultCount() {
return (this.adultCount);
}
public void setAdultCount(short adultCount) {
this.adultCount = adultCount;
}
public short getChildCount() {
return (this.childCount);
}
public void setChildCount(short childCount) {
this.childCount = childCount;
}
public short getInfantCount() {
return (this.infantCount);
}
public void setInfantCount(short infantCount) {
this.infantCount = infantCount;
}
public static class TripInfo {
private List<JacksonFlightItineraryPrice.FlightInfoParameter> flightInfoParameters;
public List getFlightInfoParameters() {
return (this.flightInfoParameters);
}
public void setFlightInfoParameters(List flightInfoParameters) {
this.flightInfoParameters = flightInfoParameters;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[\n");
if(null != flightInfoParameters){
sb.append(" flightInfoParameters:\n");
boolean isFirst = true;
for(FlightInfoParameter f : flightInfoParameters){
if(!isFirst){
sb.append(",\n");
}
sb.append(f);
isFirst = false;
}
sb.append("\n");
}
else {
sb.append(" flightInfoParameters=null\n");
}
sb.append("]");
return sb.toString();
}
}
public static class FlightInfoParameter {
private String from;
private String to;
private String fromSchedule;
private String toSchedule;
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public String getFromSchedule() {
return fromSchedule;
}
public void setFromSchedule(String fromSchedule) {
this.fromSchedule = fromSchedule;
}
public String getToSchedule() {
return toSchedule;
}
public void setToSchedule(String toSchedule) {
this.toSchedule = toSchedule;
}
#Override
public String toString() {
return "User [ "+from+"(" + fromSchedule + ") -> " + to + "(" + toSchedule + ") ]";
}
}
public static class PaxFare {
private String paxType;
private Double baseFare;
private Double totalFeesAndTaxes;
private Double totalAmount;
public String getPaxType() {
return paxType;
}
public void setPaxType(String paxType) {
this.paxType = paxType;
}
public Double getBaseFare() {
return baseFare;
}
public void setBaseFare(Double baseFare) {
this.baseFare = baseFare;
}
public Double getTotalFeesAndTaxes() {
return totalFeesAndTaxes;
}
public void setTotalFeesAndTaxes(Double totalFeesAndTaxes) {
this.totalFeesAndTaxes = totalFeesAndTaxes;
}
public Double getTotalAmount() {
return totalAmount;
}
public void setTotalAmount(Double totalAmount) {
this.totalAmount = totalAmount;
}
#Override
public String toString() {
return "User [paxType=" + paxType + ", baseFare=" + baseFare + ", totalFeesAndTaxes" + totalFeesAndTaxes + ", totalAmount=" + totalAmount + "]";
}
}
#Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[\n");
sb.append(" tripType=" + tripType + ",\n");
sb.append(" tripInfos=" + tripInfos + ",\n");
sb.append(" adultFare=" + adultFare + ",\n");
sb.append(" childFare=" + childFare + ",\n");
sb.append(" infantFare=" + infantFare + ",\n");
sb.append(" adultCount=" + adultCount + ",\n");
sb.append(" childCount=" + childCount + ",\n");
sb.append(" infantCount=" + infantCount + "\n]");
return sb.toString();
}
}
Main Application (JacksonExample.java)
package com.jgtt.samples;
import java.util.*;
import java.io.File;
import java.io.IOException;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.type.TypeReference;
import org.codehaus.jackson.map.type.CollectionType;
import org.codehaus.jackson.map.type.TypeFactory;
public class JacksonExample {
private static void flightJsonToPojo(){
ObjectMapper mapper = new ObjectMapper();
try {
String file_url = "d:\\Work files\\Java Test Codes\\Jackson\\flightItineraryPrice.json";
File jsonFile = new File(file_url);
JacksonFlightItineraryPrice flightInfo = mapper.readValue(jsonFile, JacksonFlightItineraryPrice.class);
System.out.println(flightInfo);
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
flightJsonToPojo();
}
}
The only part in the json data that I am having problem with is the "tripInfos" because its value is an arraylist of objects. If I remove tripInfos... everything works fine... but I really needed it also.
Hope somebody can show me the way :)
The class JacksonFlightItineraryPrice has one attribute tripInfos that isn't an array:
#JsonProperty("tripInfos")
private JacksonFlightItineraryPrice.TripInfo tripInfos;
and TripInfo has flightInfoParameters as a List of List<JacksonFlightItineraryPrice.FlightInfoParameter>
But in your json tripInfos is an array of JacksonFlightItineraryPrice.FlightInfoParameter.
Instead it should be:
{
"tripType": "OneWay",
"tripInfos": {
"flightInfoParameters" : [
{
"from":"EARTH",
"to":"MOON",
"fromSchedule":"2015-12-21T04:30:00",
"toSchedule":"2015-12-21T06:50:00"
},
{
"from":"MOON",
"to":"MARS",
"fromSchedule":"2015-12-21T03:30:00",
"toSchedule":"2015-12-21T011:10:00"
},
{
"from":"VENUS",
"to":"KEPLER",
"fromSchedule":"2015-12-21T01:30:00",
"toSchedule":"2015-12-21T22:30:00"
},
{
"from":"EARTH",
"to":"SUN",
"fromSchedule":"2015-12-20T02:30:00",
"toSchedule":"2015-12-29T15:10:00"
}
]
},
"adultFare":{
"paxType":"ADT",
"baseFare":"1000",
"totalFeesAndTaxes":"300",
"totalAmount":"1300.00"
},
"childFare":{
"paxType":"CHD",
"baseFare":"750",
"totalFeesAndTaxes":"250",
"totalAmount":"1000.00"
},
"infantFare":{
"paxType":"INF",
"baseFare":"250",
"totalFeesAndTaxes":"25",
"totalAmount":"275.00"
},
"adultCount":"1",
"childCount":"1",
"infantCount":"2"
}
or if you cannot change the JSON, then in JacksonFlightItineraryPrice the property tripInfos should be a list of FlightInfoParameter :
#JsonProperty("tripInfos")
private List<JacksonFlightItineraryPrice.FlightInfoParameter> tripInfos;
When i try my code below the list isn't be populated with the specific arrays please help i'm pretty new to coding GUI in netbeans
private void bookingListJCBActionPerformed(java.awt.event.ActionEvent evt) {
for(int i = 0;i<dataSource.getBookingList().size();i++){
Bookings tempBooking = dataSource.getBookingList().get(i);
boolean tempFinish = tempBooking.getFinish();
String tempMechanic = tempBooking.getMechanic();
String tempClerk = tempBooking.getClerk();
String tempService = tempBooking.getService();
if(tempFinish == true){
bookingListJCB.addItem(tempBooking);
mechanicJTF.setText(tempMechanic);
seriveceClerkJTF.setText(tempMechanic);
serviceJTF.setText(""+tempService );
finishJTF.setText(""+tempFinish);
}
}
// TODO add your handling code here:
}
Here is the Bookings class how i don't know why the combo box isn't displaying anything
public class Bookings {
private String vehicle;
private String clerk;
private String service;
private String mechanic;
private boolean finish;
public Bookings() {
}
public Bookings(String vehicle, String clerk, String service, String mechanic, boolean finish) {
this.vehicle = vehicle;
this.clerk = clerk;
this.service = service;
this.mechanic = mechanic;
this.finish = finish;
}
public String getVehicle() {
return vehicle;
}
public void setVehicle(String vehicle) {
this.vehicle = vehicle;
}
public String getClerk() {
return clerk;
}
public void setClerk(String clerk) {
this.clerk = clerk;
}
public String getService() {
return service;
}
public void setService(String service) {
this.service = service;
}
public String getMechanic() {
return mechanic;
}
public void setMechanic(String mechanic) {
this.mechanic = mechanic;
}
public boolean getFinish() {
return finish;
}
public void setFinish(boolean finish) {
this.finish = finish;
}
#Override
public String toString() {
return "Bookings{" + "vehicle=" + vehicle + ", clerk=" + clerk + ", service=" + service + ", mechanic=" + mechanic + ", finish=" + finish + '}';
}
}
I have a question object that has 4 answer objects inside.
In question.java I have a method that is:
public Answer getA() {
return a;
}
and in another method I have:
if (questions.get(randomNum).getA().isCorrect())
System.out.println("Correct!");
where questions is an arraylist containing my question objects.
This gives me a "Cannot resolve method getA()" error and im not quiet sure why.
For reference,
System.out.println(questions.get(randomNum));
works perfectly fine in printing out the question and the answers.
Question.java
public class Question {
private String questionText;
private Answer a, b, c, d;
public Question(String questionText, Answer a, Answer b, Answer c, Answer d) {
this.questionText = questionText;
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}
public String getQuestionText() {
return questionText;
}
public void setQuestionText(String questionText) {
this.questionText = questionText;
}
public Answer getA() {
return a;
}
public void setA(Answer a) {
this.a = a;
}
public Answer getB() {
return b;
}
public void setB(Answer b) {
this.b = b;
}
public Answer getC() {
return c;
}
public void setC(Answer c) {
this.c = c;
}
public Answer getD() {
return d;
}
public void setD(Answer d) {
this.d = d;
}
public String toString() {
return questionText +
"\nA) " + a +
"\nB) " + b +
"\nC) " + c +
"\nD) " + d;
}
}
Answer.Java
public class Answer {
private String answerText;
private boolean correct;
public Answer(String answerText) {
this.answerText = answerText;
this.correct = false;
}
public String getAnswerText() {
return answerText;
}
public void setAnswerText(String answerText) {
this.answerText = answerText;
}
public boolean isCorrect() {
return correct;
}
public void setCorrect() {
this.correct = true;
}
public String toString() {
return answerText;
}
}
Make sure your container (using generics) holds the Question type:
ArrayList<Question> questions = new ArrayList<Question>();
That way Java knows which method to call.
In my case, in Intellij check the Source Root Folder is set correctly.