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 7458011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T13:23:41+00:00 2026-05-29T13:23:41+00:00

When I use JSON-Lib project, I can create with the command JSONObject jObject =

  • 0

When I use JSON-Lib project, I can create with the command

JSONObject jObject = new JSONObject();
jObject.accumulate("articles", list);
String json = jObject.toString();

The following Output:

{
    "articles": [
        {
            "amount": "50",
            "id": "1",
            "pct": "50,00",
            "price": "162,37",
            "startamount": "100",
            "stockvalue": "8118,45"
        },
        {
            "amount": "20",
            "id": "2",
            "pct": "20,00",
            "price": "164,83",
            "startamount": "100",
            "stockvalue": "3296,60"
        },
        {
            "amount": "20",
            "id": "3",
            "pct": "20,00",
            "price": "170,40",
            "startamount": "100",
            "stockvalue": "3408,00"
        },...
]
}

Meanwhile I use the Gson project and are tied to it.
But the following code:

Gson gson = new Gson();
String json = gson.toJson(list);

will give me this output:

[
    {
        "id": "1",
        "amount": "50",
        "startamount": "100",
        "pct": "50,00",
        "price": "162,37",
        "stockvalue": "8118,45"
    },
    {
        "id": "2",
        "amount": "20",
        "startamount": "100",
        "pct": "20,00",
        "price": "164,83",
        "stockvalue": "3296,60"
    },...]

The elements in my list are objects from my pojo article

public class Article implements IsSerializable {
    private String id;
        private String amount;
        private String startamount;
        private String pct;
        private String price;
        private String stockvalue;

        public Article(){

        }

        //Setter & Getter
}

How can I create with the help of Gson a JSON string like the one JSON-Lib will create (JSONObject with JSONArray which inlcudes JSONObjects).
In future the JSON String will/must be extended with more arrays (not articles, but other stuff).

I retrieve this generated JSON String from a HttpServlet. Does it make sense to deserialize the Gson JSON String back to ArrayList or should I use the GWT JavaScript Overlay Types?

Thanks & BR,
mybecks

  • 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-29T13:23:42+00:00Added an answer on May 29, 2026 at 1:23 pm

    It happens because you give to the Gson a list of objects. So if you want to have the same output as in first json snippet, you have to define object, which will hold the list of articles. E.g. :

    public class ArticlesResult {
       private List<Article> articles;
    
    
       ...//the rest of the code, like getters and setters
    }
    

    Regarding converting JsArray back to the ArrayList. It depends, most of the times it is better to use overlay types, since they are faster to create, and have small footprint in generated JS code. But it is true only for production mode. In devmode, overlay types are slower, so if you rapidly access overlay types, you will waste a lot of time there.

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

Sidebar

Related Questions

I can build my project with the following command... csc /reference:lib\Newtonsoft.Json.dll SomeSourceFile.cs ... but
Can I use Jackson instead of JSON-lib with Groovy's HTTPBuilder when setting the body
I know how to use JSON to create objects, but there doesn't seem to
Why should I use JSON with ASP.NET? Can you give a practical example? I
I started to use Json.NET to convert a string in JSON format to object
I usually use JSON with jQuery to just return a string with html. However,
I use JSON to encode an array, and I get a string like this:
I am trying to use the JSON-lib api in my code to convert a
Trying to use a lib but getting this error... npm ERR! JSON.parse Failed to
How to use fastJSON (or some other JSON lib, possibly) to dump some data

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.