0

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?

pshaun
  • 47
  • 6
  • 1
    Is that your original Json data ?.If that is the case then the Json data is not in the correct format. – Sunil Sunny Aug 14 '19 at 02:54
  • Sorry I edited the original question with the original json data presented by the server. Is it possible to just get the value of the author for book_one? – pshaun Aug 14 '19 at 17:07
  • Your class called Result doesn't seems to match with your JSON data. Search on online for JSON to POJO class. You will find many links for creating the POJO class .Use any of those links to create the correct POJO class. – Sunil Sunny Aug 14 '19 at 23:48

1 Answers1

0

Try Like this

JSONObject books= obj.getJSONObject("data");
JSONObject book = books.getJSONObject("book_one");
String authName = book.getString("author");
System.out.println(authName );
KIRAN CSN
  • 121
  • 9