android - Parse JSON file with Gson -


i have method gets strongest wifi acces points signal, avialabe , returns ssid string, these ssid strings stored in raw folder in json file: how can access file in raw folder , parse gson example route_number 6 if 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 mainactivity:

             //parse storeed json file"ssid_number" , route_number back.         private int parse_ssid(string route_string) {             // todo auto-generated method stub             inputstream = 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;  	}  }

i tried code, , made modification. worked me. checking purpose, have hardcoded json string. can read raw or assets folder , 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 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 this: (actually, while parsing json using gson, json string should single object, not 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 worked.


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

order - Notification for user in user account opencart -