如何分别将“id”和“name”的值存储为var newflags = '<%= newflag %>';
console.log(newflags);
if (newflags === "null" ) {
document.getElementById('newflag').style.visibility = "hidden";
}
else if(newflags === "true" ){
document.getElementById('newflag').style.visibility = "visable";
}
?
目前,转出HashMap<Integer, String>
会给我pseudoStations
。迭代将为每个列表元素提供[{"id":0,"name":"London"}, {"id":1,"name":"Nottingham"}]
。
主要类
JsonObject
电台经理班级
public static void main(String[] args) {
Gson gson = new Gson();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("stations.json"));
StationManager sm = gson.fromJson(br, StationManager.class);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (br != null) {
closeBufferedReader(br);
}
}
}
stations.json
public class StationManager {
@SerializedName("pseudoStations")
@Expose
private List<JsonObject> pseudoStations = null;
public void setPseudoStations(List<JsonObject> pseudoStations) {
this.pseudoStations = pseudoStations;
}
}
答案 0 :(得分:0)
您可以从StationManager循环遍历JSONObject列表并将数据添加到HashMap。
public static void main(String[] args) {
Map<Integer, String> output = new HashMap<Integer, String>();
Gson gson = new Gson();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("stations.json"));
StationManager sm = gson.fromJson(br, StationManager.class);
List<JsonObject> listOfObjs = sm.getPseudoStations();
for(JsonObject obj: listOfObjs){
output.put(obj.getInt("id"), obj.getString("name"));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (br != null) {
closeBufferedReader(br);
}
}
}