Android: How to parse JSON file with Gson Library -
i have code parse json file in raw folder. encountering problem method parse_ssid(ssid)
not being called , getting @ moment 0 in xml file?!
i getting result ssid variable. since mapping variable in json file, have parse route_number value of it.
this part of method.
if (updatedresults.size() > 0) { string ssid = deliverbestaccesspoint(updatedresults); //retrieve data json string , parse it. int route_number = parse_ssid(ssid); // here getting in xml file 0 , cursor not being jumped "parse_ssid" method. textwifi.settext(string.valueof(route_number)); }
parse_ssid method:
private int parse_ssid(string ssid) { int route_number=0; inputstream raw = getresources().openrawresource(r.raw.ssid_number); reader rd = new bufferedreader(new inputstreamreader(raw)); gson gson = new gson(); wifijsonlist obj = gson.fromjson(rd, wifijsonlist.class); // iterate through list list<wifijson> wifijson = obj.getwifilist(); iterator iterator = wifijson.iterator(); while (iterator.hasnext()) { wifijson wifielement = (wifijson) iterator.next(); string ssid_name = wifielement.getssid(); if(ssid_name.equals(ssid)){ route_number = wifielement.getroute_number(); } } return 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; } }
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; } public string getssid() { return ssid; } private void setssid(string ssid) { this.ssid = ssid; } public 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 + "]"; } }
ssid_number.txt:
{ "data": [ { "ssid": "kd privat", "route_number": 1 }, { "ssid": "kd wlan hotspot", "route_number": 4 }, { "ssid": "fr wlan", "route_number": 6 } ] }
Comments
Post a Comment