1

I have a below JSON document which I need to parse. I am showing only two objects in files array. In general I will have more than 500 objects.

{  
   "files":[  
      {  
         "name":"/testing01/partition_395.shard",
         "max_chain_entries":20,
         "partition":"297, 298",
         "new_users":"123, 345, 12356"
      },
      {  
         "name":"/testing02/partition_791.shard",
         "max_chain_entries":20,
         "partition":"693, 694, 695",
         "new_users":"3345, 6678, 34568"
      }
   ]
}

And here is my DataModel class for above object -

public class JsonResponseTest {
    private String name;
    private String max_chain_entries;
    private String partition;
    private String new_users;

    // getters here
}

I need to extract all the new_users if name tag has /testing01 and populate it in HashSet in Java. I am using GSON for JSON Serialization.

private static RestTemplate restTemplate = new RestTemplate();
private static final Gson gson = new Gson();

public static void main(String[] args) {

    String jsonResponse = restTemplate.getForObject(
            "some_url", String.class);

    Type collectionType = new TypeToken<List<JsonResponseTest>>() {}.getType();
    List<JsonResponseTest> navigation = gson.fromJson(jsonResponse, collectionType);

    System.out.println(navigation);
}

But the above code is giving me error message as -

Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

Anything wrong I am doing here?

3
  • 4
    The issue is that you don’t have a JSON array. You have a (single) JSON object that has a "files" attribute whose value is a JSON array. And your code is expecting a raw JSON array. Commented Sep 3, 2014 at 20:31
  • 1
    Minor point: A JSON string is "serialized". To convert it to internal representation for the language you're using you "deserialize" it. To convert back to JSON string you "serialize". Commented Sep 3, 2014 at 20:39
  • And go to json.org to learn the JSON syntax. It only takes 5-10 minutes to learn and it will help keep you from embarrassing yourself next time. Commented Sep 3, 2014 at 20:40

1 Answer 1

2

Issue is you have a JSON Object first and then JSON Array but you are de-serializing thinking that it is JSON Array. Try below code -

String jsonResponse = restTemplate.getForObject("some_url", String.class);

Type collectionType = new TypeToken<List<JsonResponseTest>>() {}.getType();

JsonObject json = new JsonParser().parse(jsonResponse).getAsJsonObject();
JsonArray jarr = json.getAsJsonObject().getAsJsonArray("files");

List<JsonResponseTest> navigation = gson.fromJson(jarr, collectionType);
System.out.println(navigation);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.