首页
学习
活动
专区
圈层
工具
发布

Gson
EN

Stack Overflow用户
提问于 2014-10-19 01:27:57
回答 1查看 99关注 0票数 0

我对REST和JSON有相当丰富的经验,但我想不出一种方法将一些JSON读取为Java对象。

响应如下:https://api.kraken.com/0/public/OHLC?pair=XBTCZEUR&interval=60

请注意其中一个名称(相关数据)是如何依赖于查询参数的。我不确定如何为Gson创建用于反序列化的Java对象,因为其中一个变量名可能会更改。

我想也许可以使用JsonReader以流的方式读取响应,但当我这样做时,我得到了403错误响应。

有什么想法吗?

EN

回答 1

Stack Overflow用户

发布于 2014-10-19 01:38:15

如果您不知道响应将包含什么,您可以始终使用map类的实现来传递给gson,正如我在这里尝试演示的那样:

代码语言:javascript
复制
public class RestResponse {

    private boolean success;
    private String errorDescription;
    private Map<String, Object> data;

    private static Gson GSON = new Gson();

    private RestResponse()
    {
        data = new HashMap<String, Object>();
    }

    public boolean isSuccess() {
        return success;
    }

    private void setSuccess(boolean success) {
        this.success = success;
    }

    public String getErrorDescription() {
        return errorDescription;
    }

    private void setErrorDescription(String errorDescription) {
        this.errorDescription = errorDescription;
    }

    public Object getData(String... nestedKeys)
    {
        List<String> nestedKeysAsList = Arrays.asList(nestedKeys);
        return getData(nestedKeysAsList);
    }

    public Object getData(List<String> nestedKeys)
    {
        String firstKey = nestedKeys.get(0);
        if(!data.containsKey(firstKey))
            throw new IllegalArgumentException("Key not found");

        Object mapValue = data.get(firstKey);

        if(!(mapValue instanceof Map))
            return mapValue;

        String finalKey = nestedKeys.get(nestedKeys.size()-1);
        if(nestedKeys.size() > 2)
        {
            for(String nextKey : nestedKeys.subList(1,nestedKeys.size()-1))
            {
                Map<String,Object> tempMap = (Map)mapValue;
                mapValue = tempMap.get(nextKey);
            }
        }

        Map<String,Object> tempMap = (Map)mapValue;
        return tempMap.get(finalKey);
    }

    private Map<String, Object> getData() {
        return data;
    }

    private void setData(Map<String, Object> map){
        this.data = map;
    }

    public static RestResponse createUnsuccessfulResponse(Exception e)
    {
        return createUnsuccessfulResponse(e.getMessage());
    }

    public static RestResponse createUnsuccessfulResponse(String reason)
    {
        RestResponse res = new RestResponse();
        res.setSuccess(false);
        res.setErrorDescription(reason);

        return res;
    }

    public static RestResponse createSuccessfulResponse(String jsonString)
    {
        Map<String, Object> jsonToDataMap = GSON.fromJson(jsonString, Map.class);
        return createSuccessfulResponseByMap(jsonToDataMap);
    }

    private static RestResponse createSuccessfulResponseByMap(Map<String, Object> jsonToDataMap)
    {
        RestResponse res = new RestResponse();
        res.setSuccess(true);
        res.setErrorDescription("Success");
        res.setData(jsonToDataMap);

        return res;
    }
}

用法示例可以在这里找到:

https://github.com/cgunduz/btcenter/blob/master/src/main/java/com/cemgunduz/utils/entity/RestResponse.java

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26442564

复制
相关文章

相似问题

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