Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6011475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:15:55+00:00 2026-05-23T02:15:55+00:00

I am trying to use Gson to deserialize a json string returned from my

  • 0

I am trying to use Gson to deserialize a json string returned from my webservice

The structure would be returned as TypeDTO[].

where TypeDTO is like

int id;
String name;
ArrayList<ItemDTO> items[] 

and ItemDTO is like

int id;
String name;
Boolean valid;

When I call the code as follows

Gson gson = new Gson();
TypeDTO[] mytypes = (TypeDTO[]) gson.fromJson(reply, TypeDTO[].class);

Everything inside the objects is null

However, If I use the

JSONArray and JSONObject to pull them out piece by piece from the org.json jars, it works fine and the fields are populated accordingly.

Any ideas as to what I’m doing wrong? is Gson extremely fast?
Or am I better to stick with what I’ve got working already?

Thanks,
David

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:15:56+00:00Added an answer on May 23, 2026 at 2:15 am

    The example Java data structure in the original question does not match the description of the JSON structure in the comment.

    The JSON is described as

    “an array of {object with an array of {object}}”.

    In terms of the types described in the question, the JSON translated into a Java data structure that would match the JSON structure for easy deserialization with Gson is

    “an array of {TypeDTO object with an array of {ItemDTO object}}”.

    But the Java data structure provided in the question is not this. Instead it’s

    “an array of {TypeDTO object with an array of an array of {ItemDTO object}}”.

    A two-dimensional array != a single-dimensional array.

    This first example demonstrates using Gson to simply deserialize and serialize a JSON structure that is “an array of {object with an array of {object}}”.

    input.json Contents:

    [
      {
        "id":1,
        "name":"name1",
        "items":
        [
          {"id":2,"name":"name2","valid":true},
          {"id":3,"name":"name3","valid":false},
          {"id":4,"name":"name4","valid":true}
        ]
      },
      {
        "id":5,
        "name":"name5",
        "items":
        [
          {"id":6,"name":"name6","valid":true},
          {"id":7,"name":"name7","valid":false}
        ]
      },
      {
        "id":8,
        "name":"name8",
        "items":
        [
          {"id":9,"name":"name9","valid":true},
          {"id":10,"name":"name10","valid":false},
          {"id":11,"name":"name11","valid":false},
          {"id":12,"name":"name12","valid":true}
        ]
      }
    ]
    

    Foo.java:

    import java.io.FileReader;
    import java.util.ArrayList;
    
    import com.google.gson.Gson;
    
    public class Foo
    {
      public static void main(String[] args) throws Exception
      {
        Gson gson = new Gson();
        TypeDTO[] myTypes = gson.fromJson(new FileReader("input.json"), TypeDTO[].class);
        System.out.println(gson.toJson(myTypes));
      }
    }
    
    class TypeDTO
    {
      int id;
      String name;
      ArrayList<ItemDTO> items;
    }
    
    class ItemDTO
    {
      int id;
      String name;
      Boolean valid;
    }
    

    This second example uses instead a JSON structure that is actually “an array of {TypeDTO object with an array of an array of {ItemDTO object}}” to match the originally provided Java data structure.

    input.json Contents:

    [
      {
        "id":1,
        "name":"name1",
        "items":
        [
          [
            {"id":2,"name":"name2","valid":true},
            {"id":3,"name":"name3","valid":false}
          ],
          [
            {"id":4,"name":"name4","valid":true}
          ]
        ]
      },
      {
        "id":5,
        "name":"name5",
        "items":
        [
          [
            {"id":6,"name":"name6","valid":true}
          ],
          [
            {"id":7,"name":"name7","valid":false}
          ]
        ]
      },
      {
        "id":8,
        "name":"name8",
        "items":
        [
          [
            {"id":9,"name":"name9","valid":true},
            {"id":10,"name":"name10","valid":false}
          ],
          [
            {"id":11,"name":"name11","valid":false},
            {"id":12,"name":"name12","valid":true}
          ]
        ]
      }
    ]
    

    Foo.java:

    import java.io.FileReader;
    import java.util.ArrayList;
    
    import com.google.gson.Gson;
    
    public class Foo
    {
      public static void main(String[] args) throws Exception
      {
        Gson gson = new Gson();
        TypeDTO[] myTypes = gson.fromJson(new FileReader("input.json"), TypeDTO[].class);
        System.out.println(gson.toJson(myTypes));
      }
    }
    
    class TypeDTO
    {
      int id;
      String name;
      ArrayList<ItemDTO> items[];
    }
    
    class ItemDTO
    {
      int id;
      String name;
      Boolean valid;
    }
    

    Regarding the remaining two questions:

    is Gson extremely fast?

    Not compared to other deserialization/serialization APIs. Gson has traditionally been amongst the slowest. The current and next releases of Gson reportedly include significant performance improvements, though I haven’t looked for the latest performance test data to support those claims.

    That said, if Gson is fast enough for your needs, then since it makes JSON deserialization so easy, it probably makes sense to use it. If better performance is required, then Jackson might be a better choice to use. It offers much (maybe even all) of the conveniences of Gson.

    Or am I better to stick with what I’ve got working already?

    I wouldn’t. I would most always rather have one simple line of code like

    TypeDTO[] myTypes = gson.fromJson(new FileReader("input.json"), TypeDTO[].class);
    

    …to easily deserialize into a complex data structure, than the thirty lines of code that would otherwise be needed to map the pieces together one component at a time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a json string like this: http://pastebin.com/ckUZadwL I'm trying to use gson to
Hello i'm trying to convert my Java Object to a Json string to use
I'm trying use mod_rewrite to rewrite URLs from the following: http://www.site.com/one-two-file.php to http://www.site.com/one/two/file.php The
Trying to use something like the below with a char array but it doesn't
Trying to use a wildcard in C# to grab information from a webpage source,
I am trying use std::copy to copy from two different iterator. But during course
Trying to use a DataGridView like the old VB6 FlexGrid, and add the coloumns
I have never done much with serialization, but am trying to use Google's gson
I'm trying to parse some JSON object strings that I'm getting using gson-1.6.jar I
i'm trying use webview to load a image from sdcard i use this path

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.