Last active
May 21, 2016 17:50
-
-
Save es0329/a0c4fc93725a8a9610996ce1da4d49f9 to your computer and use it in GitHub Desktop.
Moshi JSON parsing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public final class League { | |
public final String franchise_id; | |
public final String url; | |
public final String name; | |
public final String franchise_name; | |
public League(String franchise_id, String url, String name, String franchise_name) { | |
this.franchise_id = franchise_id; | |
this.url = url; | |
this.name = name; | |
this.franchise_name = franchise_name; | |
} | |
@Override public String toString() { | |
return "League{" + | |
"franchise_id='" + franchise_id + '\'' + | |
", url='" + url + '\'' + | |
", name='" + name + '\'' + | |
", franchise_name='" + franchise_name + '\'' + | |
'}'; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Leagues { | |
public final League leagues; | |
public Leagues(League leagues) { | |
this.leagues = leagues; | |
} | |
@Override public String toString() { | |
return "Leagues{" + | |
"leagues=" + leagues + | |
'}'; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I/SearchModel$override: #searchLeagues. rawJson = { | |
"version": "1.0", | |
"leagues": { | |
"league": { | |
"franchise_id": "0002", | |
"url": "http://www.espn.com/fanasty_football", | |
"name": "Fanstyle Dynasty", | |
"franchise_name": "The Lovable Losers" | |
} | |
}, | |
"encoding": "utf-8" | |
} | |
I/SearchModel$override: #searchLeagues. league.franchise_name = null | |
W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.xxxxxx.xxxx.Leagues.toString()' on a null object reference | |
W/System.err: at com.xxxxxx.xxxx.SearchModel$override.searchLeagues(SearchModel.java:26) | |
W/System.err: at com.xxxxxx.xxxx.SearchModel$override.access$dispatch(SearchModel.java) | |
W/System.err: at com.xxxxxx.xxxx.SearchModel.searchLeagues(SearchModel.java:0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "1.0", | |
"leagues": { | |
"league": { | |
"franchise_id": "0002", | |
"url": "http://www.espn.com/fanasty_football", | |
"name": "Fanstyle Dynasty", | |
"franchise_name": "The Lovable Losers" | |
} | |
}, | |
"encoding": "utf-8" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.squareup.moshi.JsonAdapter; | |
import com.squareup.moshi.Moshi; | |
public class SearchModel implements LeagueSearchMvp.Model { | |
@Override public Leagues searchLeagues() { | |
Leagues result = null; | |
try { | |
String rawJson = JsonAssetReader.getStringFromFile("my_leagues.json"); | |
Moshi moshi = new Moshi.Builder().build(); | |
JsonAdapter<Leagues> jsonAdapter = moshi.adapter(Leagues.class); | |
Timber.i("#searchLeagues. rawJson = %s", rawJson); // correct JSON key-values confirmed | |
result = jsonAdapter.fromJson(rawJson); | |
Timber.i("#searchLeagues. result = %s", result.toString()); // logs JSON payload with 'null' values | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fixed!! The
leagues
property inLeagues.java
should've been of typeMap<String, League>
.