简体   繁体   English

使用GWT覆盖类型将JSON字符串转换为POJO

[英]Using GWT Overlay type to convert JSON String to POJO

this is the String I need to convert to a POJO for easy access in my GWT app: 这是我需要转换为POJO以便在我的GWT应用程序中轻松访问的String:

{"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 看着这个答案: 用gwt 2.0解析json

It seems that its quite easy to do with Overlay types. 看起来,使用叠加类型非常容易。 However the sample there shows for example getting an ID: 但是,该示例显示了例如获取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. 问题是我的GWT应用程序可能获取字段顺序的JSON字符串可能会更改。 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]); 我不是Java语言专家,但是此代码显示如何获取在解析的第一个字段上获取的ID: 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. 就我而言,如果JSON字符串中ID字段的位置发生变化,该怎么办。

Your JSON is: 您的JSON是:

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

Just create an overlay for it (ie, 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. 只需使用JavaScript对象JSNI语法为其创建一个覆盖 (即,一个零开销的Java对象,该对象代表并精确映射您的JSON结构),然后使用JsonUtils.safeEval()安全地评估有效负载并返回覆盖的实例。

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...
}

If you are just trying to get an int from an object using GWT overlay types, try this: 如果您只是尝试使用GWT覆盖类型从对象获取整数,请尝试以下操作:

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 . 其中children将是称为data的元素内的数组。

You have many options. 您有很多选择。

If you want to parse JSON in js just check this post . 如果您想在js中解析JSON, 请检查此文章 Use JSON.parse() if your client supports it or a javascript json lib. 如果您的客户端支持JSON.parse()或使用javascript json库,请使用JSON.parse() Then myJsonObject.title will return "test" , not depending of its position in the json. 然后myJsonObject.title将返回"test" ,而不取决于它在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. 您还可以使用eval()这是一个本地Js函数,但是如果不确定JSON的来源,可能会执行恶意代码。

But I would rather go with a GWT compatible utils like JSONParser . 但是我宁愿使用GWT兼容的utils,例如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()). 出于相同的原因,请谨慎使用parseStrict()(解析器内部机制也使用eval())。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM