J'ai une table commeCréation JSON correctement à partir de SQL Resultset en utilisant Java
Country State
USA Texas
USA Alasca
India Delhi
India Bombay
India Madras
Russia Mosco
Russia Petersberg
Germany Berlin
China Beijing
China Tibet
Turkey Antioch
Je dois montrer ces éléments dans deux boîtes de sélection, Pays et de l'État. Lorsque les États-Unis sont sélectionnés dans la zone de sélection Pays, Texas et Alasca doivent être affichés dans la zone de sélection État. donc je besoin de créer un objet JSON comme
[{Country:USA, State:{{statename:Texas},{statename:Alasca}}},
{Country:India, State:{{statename:Delhi}, {statename:Bombay}, {statename:Madras}}},
{Country:Germany, State:{{statename:Berlin}},
{Country:Russia, State:{{statename:Mosco},{statename:Petersberg}}},
{Country:China, State:{{statename:Beijing}, {statename:Tibet}}},
{Country:turkey, State:{{statename:Antioch}}}
]
Actuellement, j'utilise deux sqls séparés pour obtenir pays et de l'état séparément. J'utilise Java classe Pays comme
public class Country implements Serializable {
String country;
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
et j'utilise DAO comme
public List<Country> extractData(ResultSet rs) throws SQLException,
DataAccessException {
List<Country> list = new ArrayList<Country>();
while (rs.next()) {
Country obj = new Country();
obj.setCountry(rs.getString(1).trim());
list.add(obj);
}
return list;
}
Je reçois l'objet comme:
[{country:USA}, {country:India}, {country:Germany}, {country:Russia}, {country:China}, {country:turkey}]
De la même manière, je crée la classe pour l'état et code pour l'extraction de données, je reçois les états comme
[ {state:Texas} {state:Alasca}, {state:Delhi}, {state:Bombay}, {state:Madras}, {state:Berlin}, {state:Mosco}, {state:Petersberg} {state:Beijing}, {state:Tibet}, {state:Antioch}]
Cela peut être montré dans des boîtes sélectionner séparément.
Mais j'ai besoin l'objet de la façon suivante pour les afficher correctement dans les boîtes de sélection.
[{Country:USA, State:{{statename:Texas},{statename:Alasca}}},
{Country:India, State:{{statename:Delhi}, {statename:Bombay}, {statename:Madras}}},
{Country:Germany, State:{{statename:Berlin}},
{Country:Russia, State:{{statename:Mosco},{statename:Petersberg}}},
{Country:China, State:{{statename:Beijing}, {statename:Tibet}}},
{Country:turkey, State:{{statename:Antioch}}}
]
Je pourrais créer une classe Java,
class CountryState{
string country;
List <State> state;
public String getCountry()
{
return country;
}
public void setCountry(String country){
this.country=country;
}
public List<State> getState(){
return state;
}
public void setState(List<State> state)
{
this.state.add(state);
}
}
class State{
String statename;
public String getStatename()
{
return statename;
}
public void setStatename(String statename){
this.statename=statename;
}
J'ai besoin de savoir comment stocker les valeurs en jeu de résultats à un tel objet et produire l'objet désiré JSON.