从Json字符串到对象列表的JSONDeserializer

时间:2013-12-30 12:07:37

标签: c# java android json windows-phone-7

我是java / android的新手:

我在wp7上使用这个对象列表,我希望传递给android,我是怎么做到的:

我的大对象c#:

public class ListCountries
{
    public List<CountriesRepresented> _countriesRepresented { get; set; }
    public List<CountriesOrigin > _countriesOrigin { get; set; }
}

我在c#中的其他两个对象:

public class CountriesRepresented
{
    public int CountryID { get; set; }
    public string Designation { get; set; }
    public string Symbol { get; set; }
    public string NomDesignationISO { get; set; }
}

public class CountriesOrigin 
{
    public int CountryID { get; set; }
    public string Designation { get; set; }
    public string Symbol { get; set; }
    public string NomDesignationISO { get; set; }
}

我的java反序列化器:

public Object[] getListCountries()
{

     try {
        HttpClient httpClient = new DefaultHttpClient();

        HttpPost post = new HttpPost(Config.WS_PATH);
        post.setHeader("content-type", "application/json; charset=UTF-8");
        post.addHeader("Client-Application","3601cfde-e440-4a84-a2cc-a402f4c7bd14");        
       HttpResponse resp = httpClient.execute(post);
       String respStr = EntityUtils.toString(resp.getEntity());

        ListCountries _listCountries = new JSONDeserializer().deserialize(ListCountries .class, respStr);

        return _listCountries;

    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

我在java中的大对象:

public class ListCountries {
public List<CountriesRepresented> _CountriesRepresented;
public List<CountriesOrigin > _CountriesOrigin ;

public List<CountriesRepresented> getCountriesRepresented() {  
    return this._CountriesRepresented;  
  }  

  public List<CountriesOrigin > getCountriesOrigin() {  
    return this._CountriesOrigin ;  
  }


  public void setCountriesRepresented (List<CountriesRepresented> CountriesRepresented) {  
    this._CountriesRepresented = CountriesRepresented;  
  } 
  public void setCountriesOrigin  (List<CountriesOrigin > CountriesOrigin ) {  
    this._CountriesOrigin  = CountriesOrigin ;  
  }

}

我的服务是在WebAPI上给我一个正确的答案示例:`{“PaisesRepresentantes”:[{“PaisID”:4,“Designacao”:“Alemanha”,“Sigla”:“DEU”,“NomDesignacaoISO”: “德国”},{ “PaisID”:21,.......

1 个答案:

答案 0 :(得分:1)

你的Java课程应该是这样的,

public class ListCountries
{
    public List<CountriesRepresented> _countriesRepresented; //Make it private if you want and then you can add getter setter function
    public List<CountriesOrigin > _countriesOrigin;
}

public class CountriesRepresented
{
    public int CountryID; //Make it private if you want and then you can add getter setter function
    public String Designation;
    public String Symbol;
    public String NomDesignationISO;
}

public class CountriesOrigin 
{
    public int CountryID; //Make it private if you want and then you can add getter setter function
    public String Designation;
    public String Symbol;
    public String NomDesignationISO;
}

现有代码的问题在于您已将变量声明为_CountriesRepresented,而应该是_countriesRepresented,即属性区分大小写,并且这些属性映射到类中声明的变量,并记得添加一个default构造函数,如果您将任何自定义构造函数添加到任何这些类