I'm trying to get a specific string value from a json response. In my case, I only need the value "George" :
{
"data": {
"book_one": {
"author": "George"
},
"book_two": {
"author": "Thomas"
},
"book_three": {
"author": "Henry",
"award": "Gold"
}
}
}
But how do I distinguish between Authors from different books if i'm only looking for the Author's name from Book One?
I tried following an answer from here Retrofit 2: Get JSON from Response body
I don't think I'm understanding it correctly since I'm getting a null value.
I've tried the following:
Call<Result> checkData(@Field("author") String author);
Created a class called Result:
@SerializedName("data")
@Expose
private String data;
@SerializedName("book_one")
@Expose
private String book_one;
@SerializedName("author")
@Expose
private String author;
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public String getBookOne() {
return book_one;
}
public void setBookOne(String book_one) {
this.book_one = book_one;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
And I tried making the following call:
call.enqueue(new Callback<Result>() {
@Override
public void onResponse(Call<Result> call, Response<Result> response) {
if(response.isSuccessful()){
response.body(); // have your all data
String author =response.body().getAuthor();
LOG.d(TAG, "author = " + author);
}else Toast.makeText(context,response.errorBody().string(),Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<Result> call, Throwable t) {
Toast.makeText(context,t.toString(),Toast.LENGTH_SHORT).show(); // ALL NETWORK ERROR HERE
}
});
All I'm getting is a null value and not the one that I'm expecting which is just "George". I realize that I also haven't specified that I'm only looking for the author under "book_one" but I'm not sure how to do so. Can anyone guide me?