5

I am trying to parse JSON file"ssid-number" in the raw folder with GSON. The parse_SSID method parses the JSON file and finds the mapping for the passed SSID string. I am getting at the momenat this error:The method getWifiList() is undefined for the type WifiJSON at this line List<WifiJSON> wifijson = obj.getWifiList();. Is my approach correct?

JSON file:

{
    "data": [
        {
            "ssid": "KD Privat",
            "route_number": 1
        },
        {
            "ssid": "KD WLAN Hotspot",
            "route_number": 4
        },
        {
            "ssid": "FR WLAN",
            "route_number": 6
        }
    ]
}

parse_SSID method in the MainActivity:

        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 class:

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 class:

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;
    }
}
n1nsa1d00
  • 856
  • 9
  • 23
Mr Asker
  • 2,300
  • 11
  • 31
  • 56
  • Wouldn't it be easier to just make JSONObject? – Longi Apr 14 '15 at 19:17
  • if it works then it is ok but I used gson because i have read it is the easiest approach. I will add another SSID and route_number About 400 pairs to the JSON file – Mr Asker Apr 14 '15 at 19:23
  • Reread [this](http://stackoverflow.com/a/29633998/2835243) answer you got earlier. `getWifiList` is a getter in the `WiFiJSONList` class. Yet, you call it on a `WifiJSON` object. – actaram Apr 14 '15 at 19:26
  • Please see this, I used it, so far so good. https://gist.github.com/dominicthomas/ec1ce6d98dc81f6db614 – Hussain KMR Behestee Aug 11 '17 at 06:40

1 Answers1

3

This:

WifiJSON obj = gson.fromJson(rd, WifiJSON.class); 

should be

WiFiJSONList obj = gson.fromJson(rd, WiFiJSONList.class);
epoch
  • 16,396
  • 4
  • 43
  • 71
Jayesh Elamgodil
  • 1,467
  • 11
  • 15