0

I am using Gson library for first time. I am making an HTTP request and pulling response (JSON response) and need to pull a specific result.

StringBuilder response;
try (BufferedReader in = new BufferedReader( new InputStreamReader(connection.getInputStream()))) {
    String line;
    response = new StringBuilder();
    while((line = in.readLine()) != null) {
        response.append(line);
    }
}

Gson gson = new GsonBuilder()
                .setPrettyPrinting()
                .create();
System.out.println(gson.toJson(response));

The response looks like this below, and I need to pull only cardBackId:

[{\"cardBackId\":\"0\",\"name\":\"Classic\",\"description\":\"The only card back you\u0027ll ever need.\",\"source\":\"startup\",\"sourceDescription\":\"Default\",\"enabled\":true,\"img\":\"http://wow.zamimg.com/images/hearthstone/backs/original/Card_Back_Default.png\",\"imgAnimated\":\"http://wow.zamimg.com/images/hearthstone/backs/animated/Card_Back_Default.gif\",\"sortCategory\":\"1\",\"sortOrder\":\"1\",\"locale\":\"enUS\"
Matheus Lacerda
  • 5,983
  • 11
  • 29
  • 45
Chris Simmons
  • 249
  • 2
  • 7
  • 19
  • Are you trying to convert a JSON string into an object? – Jason Jun 18 '18 at 22:13
  • gson.toJson(response) is pulling the full response. I only want to pull the "cardBackId". – Chris Simmons Jun 18 '18 at 22:14
  • StringBuilder resp; try (BufferedReader in = new BufferedReader( new InputStreamReader(connection.getInputStream()))) { String line; resp = new StringBuilder(); while((line = in.readLine()) != null) { resp.append(line); } } – Chris Simmons Jun 18 '18 at 22:16
  • `toJson()` converts an object to a JSON string. `fromJson()` converts a JSON string to an object. – Jason Jun 18 '18 at 22:24

1 Answers1

1

You could use JSONPath (which is a Java library for selecting parts of a JSON object) to extract just the part you need from the string.

Alternatively, you could write a class that only contains the field you want:

public class CardBackIdResponse {
    public int cardBackId;
}

And then use Gson to unmarshall the JSON into your object:

CardBackIdResponse[] cardBackIdResponses = gson.fromJson(response.toString(), CardBackIdResponse[].class);
System.out.println("cardBackId = " + cardBackIdResponses[0].cardBackId);

When unmarshalling an object from JSON, if Gson cannot find a field in the object to populate with a value from the JSON, it will just discard the value. That's the principle we could use here.

Edit: Altered answer above to handle JSON array as per this SO question.

Jason
  • 11,744
  • 3
  • 42
  • 46
  • More docs on how to use Gson: https://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/ – Jason Jun 18 '18 at 22:30
  • I receive error below when using your suggested solution. Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $ – Chris Simmons Jun 18 '18 at 23:37
  • Edited answer to handle that `response` is actually a JSON array. – Jason Jun 19 '18 at 02:52
  • I was able to resolve. CardResponse[] list = gson.fromJson(response.toString(), CardResponse[].class); System.out.println(gson.toJson(list[1])); – Chris Simmons Jun 19 '18 at 03:38