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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:36:15+00:00 2026-05-16T06:36:15+00:00

i’m trying to parse JSON coming from a flow in my gwt 2.0 application.

  • 0

i’m trying to parse JSON coming from a flow in my gwt 2.0 application.

What is the best way ? Should I use javascriptobject ? JSonParser ? I’m lost with what I’m founding on the web because there’s never the gwt version.

String text = "{\"item\":[{\"Id\":\"1\",\"Name\":\"Bob\"},{\"Id\":\"2\",\"Name\":\"John\"},{\"Id\":\"3\",\"Name\":\"Bill\"}]}";

How can I play with my list of items ?

Thanks in advance for any help

  • 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-16T06:36:15+00:00Added an answer on May 16, 2026 at 6:36 am

    The answer depends on how much you trust that JSON 🙂 Sure, it might be coming from your application, but if insert some untrusted user input, you are facing a possible security hole.

    So:

    • for JSONs from trusted sources, I use JavaScript Overlay Types. They make integrating JSON with GWT seamless and I’d definitely recommend this approach. However, internally, this calls the eval() function which means (at least) two things: JSON parsing will be extremely fast (it uses browsers native code for that) and will be possibly insecure. Google for more info about JSON related security issues. JSONParser can also parse JSON via eval(), when you invoke its parseLenient(String jsonString) method, but it’s definitely less attractive than JSO.
    • for untrusted sources/input, you should use JSONParser via JSONParser.parseStrict(String jsonString) (available in GWT >=2.1) – you’ll have to write more code that way, but you can be sure that the input is properly handled. You might also look into integrating the “official” JSON parser from json.org with JSO – write a JSNI function that returns the parsed object and cast it to your JSO – in theory it should work 😉 (that’s what GWT does internally with JSOs, at least from what I understood)

    As for accessing lists in JSON, there are appropriate classes for that: JsArray (generic, for lists of other JSOs), JsArrayString, etc. If you look at their implementation, they are just JSNI wrappers around the native JS arrays, so they are very fast (but limited, for some reason).


    Edit in response to Tim’s comment:

    I wrote a simple abstract class that helps to minimize the boilerplate code, when dealing with JSOs and JSON:

    import com.google.gwt.core.client.JavaScriptObject;
    
    public abstract class BaseResponse extends JavaScriptObject {
        // You can add some static fields here, like status codes, etc.
    
        /**
         * Required by {@link JavaScriptObject}
         */
        protected BaseResponse() { }
    
        /**
         * Uses <code>eval</code> to parse a JSON response from the server
         * 
         * @param responseString the raw string containing the JSON repsonse
         * @return an JavaScriptObject, already cast to an appropriate type
         */
        public static final native <T extends BaseResponse> T getResponse(String responseString) /*-{
            // You should be able to use a safe parser here
            // (like the one from json.org)
            return eval('(' + responseString + ')');
        }-*/;
    }
    

    Then you write your actual JSO as such:

    import com.example.client.model.User;
    
    public class LoginResponse extends BaseResponse {
    
        protected LoginResponse() { }
    
        public final native String getToken() /*-{
            return this.t;
        }-*/;
    
        public final native int getId() /*-{
            return parseInt(this.u[0]);
        }-*/;
    
        // ...
    
        // Helper method for converting this JSO to a POJO
        public final User getUser() {
            return new User(getLogin(), getName(), getLastName());
        }
    }
    

    And finally in your code:

    // response.getText() contains the JSON string
    LoginResponse loginResponse = LoginResponse.getResponse(response.getText());
    // ^ no need for a cast \o/
    

    Your JSON looks like this (courtesy of JSONLint, a great JSON validator):

    {
        "item": [
            {
                "Id": "1",
                "Name": "Bob"
            },
            {
                "Id": "2",
                "Name": "John"
            },
            {
                "Id": "3",
                "Name": "Bill"
            }
        ]
    }
    

    So, I’d write a JSO that describes the items of that list:

    public class TestResponse extends BaseResponse {
    
        protected TestResponse() { }
    
        public final native String getId() /*-{
            return this.Id;
        }-*/;
    
        public final native String getName() /*-{
            return this.Name;
        }-*/;
    
        // Static helper for returning just the list
        // Code untested but you should get the idea ;)
        public static final native JsArray<TestResponse> getTestList(String json) /*-{
            var stuff = eval('(' + json + ')');
                return stuff.item;
        }-*/;
    }
    

    Then, in your code you call TestResponse.getTestList(someJsonString) and play around with the JsArray you get (the TestResponses it contains are created automagically). Cool, eh? 😉 It might be a bit confusing at first, but believe me, it will make sense once you start using it and it’s a lot easier than parsing via JSONParser >_>

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I am currently running into a problem where an element is coming back from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.