1

I have a small issues when I try to parse the below JSON arrays

{
  "category": [
    {
      "id": "1", 
      "idRef": "1", 
      "name": "projet", 
      "products": [
        {
          "category": "1", 
          "content": "produit du projet , et sa description", 
          "id": "5", 
          "name": "ProdProj", 
          "price": "10000.0"
        }, 
        {
          "category": "1", 
          "content": "application de pokemon potoo", 
          "id": "7", 
          "name": "pokedex", 
          "price": "10000.0"
        }, 
        {
          "category": "1", 
          "content": "description du projet n2", 
          "id": "8", 
          "name": "projet2", 
          "price": "100.0"
        }, 
        {
          "category": "1", 
          "content": "Construisez  de vos reve", 
          "id": "9", 
          "name": "Pokedex Dream", 
          "price": "100.0"
        }
      ]
    }, 
    {
      "id": "2", 
      "idRef": "2", 
      "name": "jeux video", 
      "products": [
        {
          "category": "2", 
          "content": "Description du projet de de tout ce qui suit", 
          "id": "6", 
          "name": "Jeux video Project", 
          "price": "10000.0"
        }, 
        {
          "category": "2", 
          "content": "The description pokedex is relouu", 
          "id": "10", 
          "name": "thePokedex", 
          "price": "100.0"
        }
      ]
    }, 
    {
      "id": "3", 
      "idRef": "3", 
      "name": "apps mobil", 
      "products": [
        {
          "category": "3", 
          "content": "description de l'application numéro2", 
          "id": "11", 
          "name": "application2", 
          "price": "100.0"
        }, 
        {
          "category": "3", 
          "content": "azerazeraze mobil", 
          "id": "12", 
          "name": "azerMobil", 
          "price": "100.0"
        }
      ]
    }
  ]
}

with this code:

    public class GsonFormatter {

        private static Gson gson = new GsonBuilder()
            .registerTypeAdapter(User.class, new UserDeserializer())
            .registerTypeAdapter(Category.class, new CategoryDeserializer())
            .registerTypeAdapter(Project.class, new ProjectDeserializer())
            .create();

        public static Gson getGson() {
            return gson;
        }
    }

And I have this method that makes an error:

"Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2"

    Public ArrayList<Category>() {
         return GsonFormatter.getGson().fromJson(json, 
             new TypeToken<List<Category>>() {}.getType());

So I have been trying all night long. But I cannot figure out what is going wrong. This is my first JSON use, so i'm sorry for this stupid question...

4
  • Gah, read that wrong - the error is telling you that in your parsing you have a field in one of your classes that is an object, but the JSON is an array. Specifically, it's products Commented Dec 16, 2013 at 1:05
  • ho you are telling me that the error is because my Category gets field array<Product> ?? please, i'm really lost.. Commented Dec 16, 2013 at 1:08
  • Voting to reopen as this is not a duplicate (at least not of the one shown). I misread the Q initially; see my answer below. Commented Dec 16, 2013 at 18:32
  • The JSON has now been edited so that all instances of products is an array; was this intentional? Commented Dec 17, 2013 at 16:23

2 Answers 2

2

The error is telling you that while parsing the JSON and mapping it to your Category class, there was a case where your class is expecting an object, but the JSON is an array.

Looking at your JSON, it's easy to see where that is. You have an array of Category objects. In one of them, products is an object, and in another it's an array:

... \"products\":[{\"category\":\"1\",\"content\":\"Jeux ... 
... \"products\":{\"category\":\"3\",\"content\":\"application ...

Edit in response to comments: IMHO, yes, the way the server is producing the JSON is broken. What it appears is that if there is only one product in a category, it returns the single product as an object, but if there are multiple products you get an array of them. Ideally you would want the server to always return an array even when there's only one product.

If you can't get the server changed, what you would need to do is handle this in your deserializer. You'd have to manually check the products field and see if it's an object and convert it to an array for your class.

Sign up to request clarification or add additional context in comments.

7 Comments

I'm using Jersey, he is the one who had creat this Json. So there is some probleme in the Json generation ? Do you have any clue what is going wrong with my serveur ?
I have Edit the Json, this one missed the 10 first characteres
See my edit to my answer. Ideally you want the server to always return an array or it's really messy.
As addition to this response that correctly points you to the solution, take a look at this stackoverflow.com/questions/19124387/…. It shows you how to write a deserializer for you case.
Thanks @giampaolo - I started on an example when I answered this but was pressed for time.
|
-2

Your above-mentioned method only apples for get a json array without attributes to be extracted.

My following method is suggested to take a json object with an array associated with.

    Gson gson = new Gson();
           String jsonOutput = "{ \"array\": [{ \"id\" : \"3\",   \"name\" : \"hello_world\"  },  { \"id\" : \"2\",    \"name\" : \"hello_world\"  }] }";    
        JsonObject  root = new JsonParser().parse(jsonOutput).getAsJsonObject();               

    JsonArray terms = root.getAsJsonArray("array");
    //JSONArray cms = jsonObject.getJSONArray("array");



    MyModel[] arr = gson.fromJson(terms.toString(), MyModel[].class);

1 Comment

His JSON is perfectly fine in terms of structure, it's an array of his Category objects. His issue is the products field inside those is being returned inconsistently by the server as either an object or an array. Without that issue his code would work perfectly as-is.

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.