2

I have amethod which returns the SSID of the strongest WiFi acces point. Tha data for the mapping is in file names"ssid_number.txt" in the raw folder. How can I parse this file in my case with GSON library to get the number 4 if the strongest WiFi access point"KD WLAN Hotspot" is?

{
    "KD Privat": 1,
    "KD WLAN Hotspot": 4,
    "treeWifi": 9,
    "cafeWifi": 5  //I have here more that 200 WIFI access point

}
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
Mr Asker
  • 2,300
  • 11
  • 31
  • 56
  • What have you tried? http://stackoverflow.com/questions/5490789/json-parsing-using-gson-for-java – RvdK Apr 14 '15 at 12:48

3 Answers3

3

I did it the next way.

I had JSON string pulled from a url. Then,

Gson gson = new Gson(); // create Gson obj
currentResponse = gson.fromJson(resultJSON, City.class); 

currentResponse is a json.toString() output.

Next, create new class for your json output with all fields corresponding to json. Look at my working code:

public class City {

@SerializedName("name")
public String cityName;

public String getCityName() {
    return cityName;
   }
}

In your case it would be like:

public class WiFi {

@SerializedName("cafeWifi")
public int wiFiAmount;

public int getWiFiAmount() {
    return wiFiAmount; 
   }
}

Get your wifi amount by this method:

WiFi wifi = new WiFi();
int a = wifi.getWiFiAmount();
Oleksandr Nos
  • 332
  • 5
  • 17
1

Looking at your data it seems your identifiers are not constant.

In this case it would work if you use a typemap. So something like this:

HashMap<String, Integer> mMap = null;
Type type = new TypeToken<HashMap<String, Integer>>() {}.getType();
mMap = new Gson().fromJson(json, type);
BoredAndroidDeveloper
  • 1,251
  • 1
  • 11
  • 26
0

Create POJO for your json response.

 public class JsonResponsePojo {

    @SerializedName("KD Privat")
    @Expose
    private Integer KDPrivat;
    @SerializedName("KD WLAN Hotspot")
    @Expose
    private Integer KDWLANHotspot;
    @Expose
    private Integer treeWifi;
    @Expose
    private Integer cafeWifi;

    /**
    * 
    * @return
    * The KDPrivat
    */
    public Integer getKDPrivat() {
    return KDPrivat;
    }

    /**
    * 
    * @param KDPrivat
    * The KD Privat
    */
    public void setKDPrivat(Integer KDPrivat) {
    this.KDPrivat = KDPrivat;
    }

    /**
    * 
    * @return
    * The KDWLANHotspot
    */
    public Integer getKDWLANHotspot() {
    return KDWLANHotspot;
    }

    /**
    * 
    * @param KDWLANHotspot
    * The KD WLAN Hotspot
    */
    public void setKDWLANHotspot(Integer KDWLANHotspot) {
    this.KDWLANHotspot = KDWLANHotspot;
    }

    /**
    * 
    * @return
    * The treeWifi
    */
    public Integer getTreeWifi() {
    return treeWifi;
    }

    /**
    * 
    * @param treeWifi
    * The treeWifi
    */
    public void setTreeWifi(Integer treeWifi) {
    this.treeWifi = treeWifi;
    }

    /**
    * 
    * @return
    * The cafeWifi
    */
    public Integer getCafeWifi() {
    return cafeWifi;
    }

    /**
    * 
    * @param cafeWifi
    * The cafeWifi
    */
    public void setCafeWifi(Integer cafeWifi) {
    this.cafeWifi = cafeWifi;
    }

    }

Read wifi -HotSpot

 Gson gson=new Gson();
 JsonResponsePojo data=gson.fromJson(responseString, JsonResponsePojo.class);
 String kdWLanHotSpot=data.getKDPrivat();
Bharatesh
  • 8,943
  • 3
  • 38
  • 67