2015-10-13 58 views
0

我是新来改造和RX Java和我试图解析以下JSON:Android的改造+ rxjava解析JSON

{ 
    "result": 
    { 
     "offset": 0, 
     "limit": 2, 
     "count": 408, 
     "sort": "", 
     "results":[ 
      { 
       "_id": "1", 
       "iid": "338", 
       "sv": "0", 
       "sd": "20000101000000", 
       "vtyp": "1", 
       "sno": "0001",    
      }, 
      { 
       "_id": "2", 
       "iid": "339", 
       "sv": "0", 
       "sd": "20000101000000", 
       "vtyp": "1", 
       "sno": "0001",   
      } 

     ] 
    } 

} 

我始载于“结果对象的数组中的兴趣”。从我已经能够找到的我应该实现JsonDeserializer来获取该数据并在构建RestAdapter时使用.setConverter。这是我现在有:

class MyDeserializer implements JsonDeserializer<Pojo> { 
    @Override 
    public Station deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { 
     JsonElement element = json.getAsJsonObject().get("results"); 
     return new Gson().fromJson(element, Pojo.class); 
    } 

我想改造GET调用返回

Observable<List<Pojo>> 

表示在 “结果” 的对象

我Pojo.class看起来是这样的:

public class Pojo { 
    public String _id; 
    public String iid; 
    public String sv; 
    public String sd; 
    public String vtyp; 
} 

现在写我尝试调用API时出现此错误,所以我认为我在Json中出错了反序列化:

retrofit.RetrofitError: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ 

任何帮助,将不胜感激!

回答

0

你需要创建下面的第三POJO类

  1. MyPojo.java

    public class MyPojo 
    { 
        private Result result; 
    
        public Result getResult() 
        { 
         return result; 
        } 
    
        public void setResult (Result result) 
        { 
         this.result = result; 
        } 
    
    } 
    
  2. Result.java

    public class Result 
    { 
    private String limit; 
    
    private String sort; 
    
    private Results[] results; 
    
    private String count; 
    
    private String offset; 
    
    public String getLimit() 
    { 
        return limit; 
    } 
    
    public void setLimit (String limit) 
    { 
        this.limit = limit; 
    } 
    
    public String getSort() 
    { 
        return sort; 
    } 
    
    public void setSort (String sort) 
    { 
        this.sort = sort; 
    } 
    
    public Results[] getResults() 
    { 
        return results; 
    } 
    
    public void setResults (Results[] results) 
    { 
        this.results = results; 
    } 
    
    public String getCount() 
    { 
        return count; 
    } 
    
    public void setCount (String count) 
    { 
        this.count = count; 
    } 
    
    public String getOffset() 
    { 
        return offset; 
    } 
    
    public void setOffset (String offset) 
    { 
        this.offset = offset; 
    } 
    

    }

  3. 结果s.java

    public class Results 
    { 
    private String sno; 
    
    private String _id; 
    
    private String iid; 
    
    private String vtyp; 
    
    private String sd; 
    
    private String sv; 
    
    public String getSno() 
    { 
        return sno; 
    } 
    
    public void setSno (String sno) 
    { 
        this.sno = sno; 
    } 
    
    public String get_id() 
    { 
        return _id; 
    } 
    
    public void set_id (String _id) 
    { 
        this._id = _id; 
    } 
    
    public String getIid() 
    { 
        return iid; 
    } 
    
    public void setIid (String iid) 
    { 
        this.iid = iid; 
    } 
    
    public String getVtyp() 
    { 
        return vtyp; 
    } 
    
    public void setVtyp (String vtyp) 
    { 
        this.vtyp = vtyp; 
    } 
    
    public String getSd() 
    { 
        return sd; 
    } 
    
    public void setSd (String sd) 
    { 
        this.sd = sd; 
    } 
    
    public String getSv() 
    { 
        return sv; 
    } 
    
    public void setSv (String sv) 
    { 
        this.sv = sv; 
    } 
    

    }

写下面的代码在你的活动

private RestAdapter restAdapter; 

private ApiListeners apiListener; 

restAdapter = new RestAdapter.Builder().setLogLevel(RestAdapter.LogLevel.FULL).setEndpoint("Your Base Url").build(); 
apiListener = restAdapter.create(ApiListeners.class); 

apiListener.getData(new Callback<MyPojo>() 
     { 
      @Override 
      public void success(MyPojo pojo, Response response) 
      { 

      } 

      @Override 
      public void failure(RetrofitError error) 
      { 

      } 
     }); 

创建下面听众

public interface ApiListeners 
{ 
    @GET("/yourApiTag") 
    public void getData(retrofit.Callback<MyPojo> responseVo); 
} 
+0

我试图避免,因为他们创造三类根本不需要。这就是我试图使用Json解串器的原因。如果按照您的建议进行操作,我可以解析数据,但如果可能的话,我希望在没有不必要的类的情况下执行此操作 –