Android: How to parse JSON file with Gson Library?

Android: How to parse JSON file with GSON library?

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

  • Answer:

    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();

Mr Asker at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

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

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();

bharat

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.