1

I have a method that gets the strongest Wifi acces points signal, which avialabe is and returns SSID string, all these SSID strings are stored in the raw folder in JSON file: How can I access the file in the raw folder and parse it with Gson to get for example the route_number 6 if the SSID "FR WLAN" is?

ssid_number JSON file:

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

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
                + "]";
    }



}

parse_SSID in the MainActivity:

             //parse the storeed json file"ssid_number" and get the route_number back.
        private int parse_SSID(String route_string) {
            // TODO Auto-generated method stub
            InputStream is = getResources().openRawResource(R.raw.ssid_number);
            Gson gson = new Gson();
            WifiJSON obj = gson.fromJson(route_string, WifiJSON.class);


            return 0;
        }
    }

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;
 }
}
Mr Asker
  • 2,300
  • 11
  • 31
  • 56

1 Answers1

1

I tried your code, and made some modification. This worked for me. For checking purpose, I have hardcoded the json string. You can read from raw or assets folder and move ahead.

    private int parse_SSID(String route_string) {
    // TODO Auto-generated method stub
    Gson gson = new Gson();
    WiFiJSONList obj = gson.fromJson(route_string, WiFiJSONList.class);
    //Now iterate through the list
    List<WiFiJSON> wifijson = obj.getWifiList();
    Iterator iterator = wifijson.iterator();
    while (iterator.hasNext()) {
        WiFiJSON wifielement = (WiFiJSON) iterator.next();
        System.out.println(wifielement.getSsid() + "----" + wifielement.getRoute_number());
    }
    return 0;
}

Usage will be like this: (Actually, while parsing json using gson, your json string should be a single object, not like an array directly.)

String json = "{\"data\": [{\"ssid\": \"KD Privat\",\"route_number\": 1},{\"ssid\": \"KD WLAN Hotspot\",\"route_number\": 4},{\"ssid\": \"FR WLAN\",\"route_number\": 6}]}";
    parse_SSID(json);


public class WiFiJSON {

private String ssid;
private int route_number;

public String getSsid() {
    return ssid;
}
public void setSsid(String ssid) {
    this.ssid = ssid;
}
public int getRoute_number() {
    return route_number;
}
public void setRoute_number(int route_number) {
    this.route_number = route_number;
}

}

public class WiFiJSONList {

private ArrayList<WiFiJSON> data;

public ArrayList<WiFiJSON> getWifiList() {
    return data;
}

public void setWifiList(ArrayList<WiFiJSON> wifiList) {
    this.data = wifiList;
}

}

Let me know if this worked.

Santhana
  • 233
  • 1
  • 5
  • Where I have to add these lines"String json = "{\"data\": [{\"ssid\": \"KD Privat\",\"route_number\": 1},{\"ssid\": \"KD WLAN Hotspot\",\"route_number\": 4},{\"ssid\": \"FR WLAN\",\"route_number\": 6}]}"; parse_SSID(json);" ? I think it would be hard to use this approach in my case because this file contains more than 200 another mapping and I have another complex JSON file which I want to parse I think it it would be hard to write all the file as string variable. – Mr Asker Apr 14 '15 at 18:05
  • For checking purpose, I have added like this. You can pass the entire json that you are getting from your stream that you are reading. Since gson needs a json object to be parsed, you can encapsulate your array with an object, like how I have modified. – Santhana Apr 14 '15 at 18:07
  • I am getting this error "The method getWifiList() is undefined for the type WifiJSON" at this line" List wifijson = obj.getWifiList();"? I changed ur spell of WiFiJSON to WifiJSON since my cass is written like this :) – Mr Asker Apr 14 '15 at 18:14
  • Have you added the WifiJSONList class as well? In that class also, there is a reference to the WiFiJSON class. Also make sure that getWifiList is returning the proper object from the WiFiJSONList class. – Santhana Apr 14 '15 at 18:22
  • Aprops I have changed my JSON file and added data identifier to it – Mr Asker Apr 14 '15 at 18:27