首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Google end在引号中返回long数据类型的JSON

Google end在引号中返回long数据类型的JSON
EN

Stack Overflow用户
提问于 2013-04-11 14:23:57
回答 1查看 1.2K关注 0票数 5

我正在为我的rest服务使用谷歌云端点。我正在使用RestyGWT在GWT客户端中使用这些数据。

我注意到云端点会自动将一个长数据类型括在双引号中,这会在我尝试将RestyGWT转换为POJO时导致异常。

以下是我的示例代码。

代码语言:javascript
复制
@Api(name = "test")
public class EndpointAPI {

@ApiMethod(httpMethod = HttpMethod.GET,  path = "test")
public Container test() {
    Container container = new Container();

    container.testLong =  (long)3234345;
    container.testDate = new Date();
    container.testString = "sathya";
    container.testDouble = 123.98;
    container.testInt = 123;                
    return container;
}
public class Container {
    public long testLong;
    public Date testDate;
    public String testString;
    public double testDouble;
    public int testInt;
}

}

这是云端点作为JSON返回的内容。您可以看到testLong被序列化为" 3234345“而不是3234345。

我有以下问题。(1)如何删除长值中的双引号?(2)如何将字符串格式改为"yyyy-MMM-dd hh:mm:ss“?

向您致敬,Sathya

EN

回答 1

Stack Overflow用户

发布于 2013-05-23 22:47:33

您使用的是什么版本的restyGWT?你有没有尝试过1.4快照?我认为这是负责解析restygwt中长整型的代码(1.4),它可能会对你有所帮助:

代码语言:javascript
复制
 public static final AbstractJsonEncoderDecoder<Long> LONG = new AbstractJsonEncoderDecoder<Long>() {

    public Long decode(JSONValue value) throws DecodingException {
        if (value == null || value.isNull() != null) {
            return null;
        }
        return (long) toDouble(value);
    }

    public JSONValue encode(Long value) throws EncodingException {
        return (value == null) ? getNullType() : new JSONNumber(value);
    }
};

 static public double toDouble(JSONValue value) {
    JSONNumber number = value.isNumber();
    if (number == null) {
        JSONString val = value.isString();
        if (val != null){
            try {
                return Double.parseDouble(val.stringValue());
            }
            catch(NumberFormatException e){
                // just through exception below
            }
        }
        throw new DecodingException("Expected a json number, but was given: " + value);
    }
    return number.doubleValue();
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15942022

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档