2

this is the String I need to convert to a POJO for easy access in my GWT app:

{"title":"test","content":"test","id":1,"user":null,"hash":null,"created":1379569937945,"modified":1379569937945,"password":null,"views":0}

Looking at this answer: Parse json with gwt 2.0

It seems that its quite easy to do with Overlay types. However the sample there shows for example getting an ID:

    public final native int getId() /*-{
        return parseInt(this.u[0]);
    }-*/;

Problem is that the JSON String that my GWT app might get the order of the fields might change. What could be done for this reason? I'm no Javascript expert but this code shows to get the ID it gets on the first field parsed: return parseInt(this.u[0]); if I understand this correctly? Like in my case what if the ID field position in the JSON string varries.

Community
  • 1
  • 1
xkm
  • 61
  • 3
  • Not really the answer to your question: You could have a look at the AutoBeans framework (https://code.google.com/p/google-web-toolkit/wiki/AutoBean). This automatically generates overlay types for Interfaces with getters and setters. And as it uses JSON for (de-)serialization it could solve your use-case much easier than manual parsing. – Steffen Schäfer Sep 19 '13 at 07:37

3 Answers3

3

Your JSON is:

{
  "title": "test",
  "content": "test",
  "id": 1,
  "user": null,
  "hash": null,
  "created": 1379569937945,
  "modified": 1379569937945,
  "password": null,
  "views": 0
}

Just create an overlay for it (i.e., a zero-overhead Java object that represents and maps exactly your JSON structure) using JavaScript objects and JSNI syntax, and use JsonUtils.safeEval() to safely evaluate the payload and return the overlayed instance.

import com.google.gwt.core.client.JsonUtils;

public class YourFancyName extends JavaScriptObject {

  /**
   * Overlay types always have protected, zero-arg ctors.
   */
  protected YourFancyName() { }

  /**
   * Safely evaluate the JSON payload and create the object instance.
   */
  public static YourFancyName create(String json) {
    return (YourFancyName) JsonUtils.safeEval(json);
  }

  /**
   * Returns the title property.
   */
  public native String getTitle() /*-{
    return this.title;
  }-*/;

  /**
   * Returns the id property.
   */
  public native int getId() /*-{
    return this.id;
  }-*/;

  // And the like...
}
Andrea Boscolo
  • 3,038
  • 1
  • 16
  • 21
1

If you are just trying to get an int from an object using GWT overlay types, try this:

public final native String getId() /*-{
    return this.id;
}-*/;

Or if you want to get an array, do this:

public final native JsArray getData() /*-{
    return this.data.children;
}-*/;

where children would be an array inside an element called data.

Churro
  • 4,166
  • 3
  • 25
  • 26
0

You have many options.

If you want to parse JSON in js just check this post. Use JSON.parse() if your client supports it or a javascript json lib. Then myJsonObject.title will return "test", not depending of its position in the json.

You can also use eval() which is a native Js function but could execute malicious code if you're not sure about the JSON's origin.

But I would rather go with a GWT compatible utils like JSONParser. This post has some useful info about it. Be careful about using parseStrict() for the same reason (the parser inner mechanism also uses eval()).

Community
  • 1
  • 1
Olivier Tonglet
  • 3,312
  • 24
  • 40