有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何将此JSON作为对象读取?格森

我对REST和JSON有相当多的经验,但我没有找到一种将JSON作为Java对象读取的方法

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

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

我认为也许使用JsonReader以流式方式读取响应可能有效,但当我这样做时,我得到了403错误响应

有什么想法吗


共 (1) 个答案

  1. # 1 楼答案

    如果您对响应包含的内容没有确切的了解,您可以始终使用map类的实现来传递给gson,正如我在这里尝试演示的:

    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