2014-10-18 49 views
0

我对REST和JSON有相当丰富的经验,但是我没有想出一种方法来读取一些JSON作为Java对象。如何读取这个JSON作为一个对象? Gson

的反应是在这里:https://api.kraken.com/0/public/OHLC?pair=XBTCZEUR&interval=60

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

我认为也许使用JsonReader以流媒体方式读取响应可能会起作用,但是当我这样做时,我得到了403错误响应。

任何想法?

+0

你可以遵循的形式给出了对bean的格式,这样的bean包含的所有元素(那些可以发现或不能在JSON源泉)还你读它json有很多参数,所以它的bette你使用bean'http://www.jsoneditoronline.org/?id = 13e7c95e8439b49c20412204ecaa1c8e' – Divya 2014-10-18 17:33:34

回答

0

如果你没有关于什么的响应将包含确切的知识,您可以随时使用地图类的实现传授给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

相关问题