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;
	}
}

4
  • read this.. stackoverflow.com/questions/5490789/… Commented Apr 14, 2015 at 17:19
  • your file contains a json array. which usually maps to a List Commented Apr 14, 2015 at 17:20
  • @Michele: I want first to access it and then aparse it and your link it is not clear for me. Moreover, my JSON file is an array. Commented Apr 14, 2015 at 17:38
  • @ njzk2 does that mean I do not need the WifiJSON class at all? Commented Apr 14, 2015 at 17:40

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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.
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.
I am getting this error "The method getWifiList() is undefined for the type WifiJSON" at this line" List<WifiJSON> wifijson = obj.getWifiList();"? I changed ur spell of WiFiJSON to WifiJSON since my cass is written like this :)
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.
Aprops I have changed my JSON file and added data identifier to it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.