我正在尝试解析JSON文件" ssid-number"在GSON的原始文件夹中。 parse_SSID方法解析JSON文件并查找传递的SSID字符串的映射。我在momenat遇到此错误:The method getWifiList() is undefined for the type WifiJSON
此行List<WifiJSON> wifijson = obj.getWifiList();
。我的方法是否正确?
JSON文件:
{
"data": [
{
"ssid": "KD Privat",
"route_number": 1
},
{
"ssid": "KD WLAN Hotspot",
"route_number": 4
},
{
"ssid": "FR WLAN",
"route_number": 6
}
]
}
MainActivity中的parse_SSID方法:
private int parse_SSID(String ssid) {
InputStream raw = getResources().openRawResource(R.raw.ssid_number);
Reader rd = new BufferedReader(new InputStreamReader(raw));
Gson gson = new Gson();
WifiJSON obj = gson.fromJson(rd, WifiJSON.class);
// Now iterate through the list
List<WifiJSON> wifijson = obj.getWifiList(); //here is my error.
Iterator iterator = wifijson.iterator();
while (iterator.hasNext()) {
WifiJSON wifielement = (WifiJSON) iterator.next();
System.out.println(wifielement.getSsid() + "----"
+ wifielement.getRoute_number());
}
}
WifiJSON课程:
public class WifiJSON {
private String ssid;
private int route_number;
public WifiJSON(String ssid, int route_number) {
this.ssid = ssid;
this.route_number = route_number;
}
private String getSsid() {
return ssid;
}
private void setSsid(String ssid) {
this.ssid = ssid;
}
private int getRoute_number() {
return route_number;
}
private void setRoute_number(int route_number) {
this.route_number = route_number;
}
@Override
public String toString() {
return "WifiJSON [ssid=" + ssid + ", route_number=" + route_number
+ "]";
}
}
WiFiJSONList类:
import java.util.ArrayList;
public class WiFiJSONList {
private ArrayList<WifiJSON> data;
public ArrayList<WifiJSON> getWifiList() {
return data;
}
public void setWifiList(ArrayList<WifiJSON> wifiList) {
this.data = wifiList;
}
}
答案 0 :(得分:3)
此:
WifiJSON obj = gson.fromJson(rd, WifiJSON.class);
应该是
WiFiJSONList obj = gson.fromJson(rd, WiFiJSONList.class);