Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android GSON/JSON反序列化并在适配器中显示_Android_Android Layout_Gson - Fatal编程技术网

Android GSON/JSON反序列化并在适配器中显示

Android GSON/JSON反序列化并在适配器中显示,android,android-layout,gson,Android,Android Layout,Gson,我已经陷入这个问题两周了,希望有人能帮我解决。提前谢谢 我想在活动中创建一个视图,该视图由标题视图和底部的列表视图组成。到目前为止,我已经成功地将数据从JSON获取到listView,因为大多数教程都只涉及JSON和listView 我的问题是,我如何才能得到下面的数据显示在标题上,一个简单的视图(非列表视图),甚至是我想要的视图 JSON文件 { "photos" : [ { "thumbnail" : "http://www.myserver.com/01.jpg", }

我已经陷入这个问题两周了,希望有人能帮我解决。提前谢谢

我想在活动中创建一个视图,该视图由标题视图和底部的列表视图组成。到目前为止,我已经成功地将数据从JSON获取到listView,因为大多数教程都只涉及JSON和listView

我的问题是,我如何才能得到下面的数据显示在标题上,一个简单的视图(非列表视图),甚至是我想要的视图

JSON文件

{
 "photos" : [
  {
     "thumbnail" : "http://www.myserver.com/01.jpg",
  },
  {
     "thumbnail" : "http://www.myserver.com/02.jpg",
  },
  {
     "thumbnail" : "http://www.myserver.com/03.jpg",
  },
],
"info" : [
  {
     "id" : "1",
     "description" : "My White Toyota",
     "country" : "Japan",
     "year" : "1982"
   }
 ];
"owners" : [
   {
     "ownerid" : "5633432",
     "name" : "Jackson",
     "year_own" : "1982-1985"
    },
    {
     "ownerid" : "76832",
     "name" : "Kurt",
     "year_own" : "1986-1995"
    },
    {
     "ownerid" : "236471",
     "name" : "Alma",
     "year_own" : "1999-2005"
    }
   ]
}
列表类

public class car_detail {
    private List<CAR_INFO_MODEL> photos;
    private List<CAR_INFO_MODEL> info;
    private List<CAR_INFO_MODEL> owners;

public List<CAR_INFO_MODEL> getPhotos(){
    return photos;
}
public void setPhotoList(List<CAR_INFO_MODEL> photos){
    this.photos = photos;
}

    public List<CAR_INFO_MODEL> getInfos(){
    return info;
}
public void setInfoList(List<CAR_INFO_MODEL> info){
    this.info = info;
}

    public List<CAR_INFO_MODEL> getOwners(){
    return owners;
}
public void setOwnerList(List<CAR_INFO_MODEL> owners){
    this.owners = owners;
}
}
activity类(我使用AsyncTask查询并获取JSON响应),我可以成功地从JSON获取info对象,但不知道如何获取照片和所有者

 public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cardetail_layout);

    lv = (ListView) findViewById(R.id.list_cardetail);

    carArray = new ArrayList<CAR_INFO_MODEL>();
    carDetailAdapter = new carDetailAdapter(carActivity.this, R.layout.caritem_layout, infoArray);
            lv.setAdapter(carDetailAdapter);  
            try {
                new DealSync().execute("http://www.myserver.com/carsquery/");
            } catch(Exception e) {}
 }


 String json = client.getResponse();
 list = new Gson().fromJson(json, car_detail.class); 

 return list;

 protected void onPostExecute(car_detail infolist) {

        for(CAR_INFO_MODEL lm : infolist.getInfo())
        {
            infoArray.add(lm);
        }
        carDetailAdapter.notifyDataSetChanged();
    }
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.cardetail_布局);
lv=(ListView)findViewById(R.id.list\u cardetail);
carArray=newarraylist();
carDetailAdapter=新的carDetailAdapter(carActivity.this、R.layout.caritem_layout、infoArray);
低压设置适配器(carDetailAdapter);
试一试{
新建DealSync()。执行(“http://www.myserver.com/carsquery/");
}捕获(例外e){}
}
字符串json=client.getResponse();
list=new Gson().fromJson(json,car_detail.class);
退货清单;
受保护的void onPostExecute(车辆详细信息列表){
对于(汽车信息车型lm:infolist.getInfo())
{
infoArray.add(lm);
}
notifyDataSetChanged();
}
适配器类(我可以获取要显示为单个项目列表的信息,但我不知道如何从数据中获取照片和所有者(底部列表视图)并将其显示。)

公共类carDetailAdapter扩展了ArrayAdapter{
智力资源;
字符串响应;
语境;
私人公寓;
公共carDetailAdapter(上下文上下文、int资源、列表对象){
超级(上下文、资源、对象);
这个资源=资源;
dInflater=LayoutInflater.from(上下文);
}
静态类视窗夹{
文本视图描述;
}
公共视图getView(int位置、视图转换视图、视图组父视图)
{
视窗座;
汽车信息模型lm=(汽车信息模型)获取项目(位置);
//夸大观点
if(convertView==null)
{
convertView=充气机充气(R.layout.carDetail_layout,空);
holder=新的ViewHolder();
holder.description=(TextView)convertView.findViewById(R.id.cardesc);
convertView.setTag(支架);
}
其他的
{
holder=(ViewHolder)convertView.getTag();
}
holder.description.setText(lm.getDesc());
返回视图;
}
}
这听起来可能很复杂,希望有人能给我帮助。非常感谢您使用

或者,如果您真的想序列化/反序列化json。

使用


或者,如果您真的想序列化/反序列化json。

我注意到您的逻辑中存在一些问题

  • 首先,您使用的JSON字符串不是JSON格式的。GSON解析器无法解析相同的字符串
问题:

  • 照片的
    缩略图
    变量末尾还有一个逗号
  • info对象以
    结尾而不是
更正的JSON字符串如下:

{
 "photos" : [
  {
     "thumbnail" : "http://www.myserver.com/01.jpg"
  },
  {
     "thumbnail" : "http://www.myserver.com/02.jpg"
  },
  {
     "thumbnail" : "http://www.myserver.com/03.jpg"
  }
],
"info" : [
  {
     "id" : "1",
     "description" : "My White Toyota",
     "country" : "Japan",
     "year" : "1982"
   }
 ],
"owners" : [
   {
     "ownerid" : "5633432",
     "name" : "Jackson",
     "year_own" : "1982-1985"
    },
    {
     "ownerid" : "76832",
     "name" : "Kurt",
     "year_own" : "1986-1995"
    },
    {
     "ownerid" : "236471",
     "name" : "Alma",
     "year_own" : "1999-2005"
    }
   ]
}
现在我发现的逻辑错误

  • 您的
    CarDetails
    JSON数据包含三个对象。1.照片列表2。一个信息对象和3。业主名单
  • 但是您的实现将其作为一个包含此类中所有数据的三个
    CAR\u INFO\u模型来处理。这样做,JSON中存在的所有分类都将丢失
  • 这是你的问题
要解决这个问题,请将数据分类为JSON表示的数据。如下所示

照片

public class Photo {

    /**
     * Constructor for Photo.
     * @param thumbnail <tt></tt>
     */
    public Photo(String thumbnail) {
        super();
        this.thumbnail = thumbnail;
    }

    private String thumbnail;

    /**
     * Gets the thumbnail.
     * 
     * @return <tt> the thumbnail.</tt>
     */
    public String getThumbnail() {
        return thumbnail;
    }

    /**
     * Sets the thumbnail.
     *
     * @param thumbnail <tt> the thumbnail to set.</tt>
     */
    public void setThumbnail(String thumbnail) {
        this.thumbnail = thumbnail;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Photo [thumbnail=" + thumbnail + "]";
    }



}
public class Info {
    /*"id" : "1",
     "description" : "My White Toyota",
     "country" : "Japan",
     "year" : "1982"

     * */

    private String id;
    /**
     * Constructor for Info.
     * @param id
     * @param description
     * @param country
     * @param year <tt></tt>
     */
    public Info(String id, String description, String country, String year) {
        super();
        this.id = id;
        this.description = description;
        this.country = country;
        this.year = year;
    }
    private String description;
    private String country;
    private String year;
    /**
     * Gets the id.
     * 
     * @return <tt> the id.</tt>
     */
    public String getId() {
        return id;
    }
    /**
     * Sets the id.
     *
     * @param id <tt> the id to set.</tt>
     */
    public void setId(String id) {
        this.id = id;
    }
    /**
     * Gets the description.
     * 
     * @return <tt> the description.</tt>
     */
    public String getDescription() {
        return description;
    }
    /**
     * Sets the description.
     *
     * @param description <tt> the description to set.</tt>
     */
    public void setDescription(String description) {
        this.description = description;
    }
    /**
     * Gets the country.
     * 
     * @return <tt> the country.</tt>
     */
    public String getCountry() {
        return country;
    }
    /**
     * Sets the country.
     *
     * @param country <tt> the country to set.</tt>
     */
    public void setCountry(String country) {
        this.country = country;
    }
    /**
     * Gets the year.
     * 
     * @return <tt> the year.</tt>
     */
    public String getYear() {
        return year;
    }
    /**
     * Sets the year.
     *
     * @param year <tt> the year to set.</tt>
     */
    public void setYear(String year) {
        this.year = year;
    }
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Info [id=" + id + ", description=" + description + ", country="
                + country + ", year=" + year + "]";
    }



}
public class CarDetail {

    private List<Photo> photos;
    private List<Info> info;
    private List<Owner> owners;
    /**
     * Gets the photos.
     * 
     * @return <tt> the photos.</tt>
     */
    public List<Photo> getPhotos() {
        return photos;
    }
    /**
     * Sets the photos.
     *
     * @param photos <tt> the photos to set.</tt>
     */
    public void setPhotos(List<Photo> photos) {
        this.photos = photos;
    }
    /**
     * Gets the info.
     * 
     * @return <tt> the info.</tt>
     */
    public List<Info> getInfo() {
        return info;
    }
    /**
     * Sets the info.
     *
     * @param info <tt> the info to set.</tt>
     */
    public void setInfo(List<Info> info) {
        this.info = info;
    }
    /**
     * Gets the owners.
     * 
     * @return <tt> the owners.</tt>
     */
    public List<Owner> getOwners() {
        return owners;
    }
    /**
     * Sets the owners.
     *
     * @param owners <tt> the owners to set.</tt>
     */
    public void setOwners(List<Owner> owners) {
        this.owners = owners;
    }
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "CarDetail [photos=" + photos + ", info=" + info + ", owners="
                + owners + "]";
    }



}
“所有者”类

public class Owner {
    /*
     * "ownerid" : "5633432",
     "name" : "Jackson",
     "year_own" : "1982-1985"

     * */

    private String ownerid;
    /**
     * Constructor for Owner.
     * @param ownerid
     * @param name
     * @param year_own <tt></tt>
     */
    public Owner(String ownerid, String name, String year_own) {
        super();
        this.ownerid = ownerid;
        this.name = name;
        this.year_own = year_own;
    }
    private String name;
    private String year_own;
    /**
     * Gets the ownerid.
     * 
     * @return <tt> the ownerid.</tt>
     */
    public String getOwnerid() {
        return ownerid;
    }
    /**
     * Sets the ownerid.
     *
     * @param ownerid <tt> the ownerid to set.</tt>
     */
    public void setOwnerid(String ownerid) {
        this.ownerid = ownerid;
    }
    /**
     * Gets the name.
     * 
     * @return <tt> the name.</tt>
     */
    public String getName() {
        return name;
    }
    /**
     * Sets the name.
     *
     * @param name <tt> the name to set.</tt>
     */
    public void setName(String name) {
        this.name = name;
    }
    /**
     * Gets the year_own.
     * 
     * @return <tt> the year_own.</tt>
     */
    public String getYear_own() {
        return year_own;
    }
    /**
     * Sets the year_own.
     *
     * @param year_own <tt> the year_own to set.</tt>
     */
    public void setYear_own(String year_own) {
        this.year_own = year_own;
    }
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Owner [ownerid=" + ownerid + ", name=" + name + ", year_own="
                + year_own + "]";
    }


}
并使用以下这些类来解析和获取存储在每个对象中的详细信息(例如,照片、所有者和信息)

String json=“{”+
“\”照片\:[“+
"  {" + 
“\“缩略图\”:\”http://www.myserver.com/01.jpg\"" + 
"  }," + 
"  {" + 
“\“缩略图\”:\”http://www.myserver.com/02.jpg\"" + 
"  }," + 
"  {" + 
“\“缩略图\”:\”http://www.myserver.com/03.jpg\"" + 
"  }" + 
"]," + 
“\”信息\“:[”+
"  {" + 
“\“id\”:\“1\”,“+
“\”描述\:“\”我的白色丰田\“,“+
“‘国家’:‘日本’,”+
“\”年“:\”1982\”+
"   }" + 
" ]," + 
“\”所有者\:[“+
"   {" + 
“\”所有者ID\”:\“5633432\”,“+
“\”姓名\“:\”杰克逊\“,“+
““自己的年份”:“1982-1985”
"    }," + 
"    {" + 
“\'ownerid\”:\'76832\”,“+
“\”姓名“:\”库尔特“,“+
“自己的年份”:“1986-1995”
"    }," + 
"    {" + 
“\'ownerid\”:\'236471\”,“+
“\”姓名“:\”阿尔玛“,“+
““自己的年份”:“1999-2005”
"    }" + 
"   ]" + 
"}";
//将JSON字符串解析为CardDetail对象
CarDetail detail=new Gson().fromJson(json,CarDetail.class);
//从CardDetail对象检索数据
List photos=detail.getPhotos();
System.out.println(“检索照片”);
(照片:照片){
系统输出打印(照片);
}
List info=detail.getInfo();
System.out.println(“检索信息”);
用于(信息2:Info){
System.out.println(info2);
}
System.out.println(“检索所有者”);
List owners=detail.getOwners();
用于(所有者:所有者){
System.out.println(所有者);
}

现在在视图中使用检索到的数据。如果您需要任何帮助,请告诉我您的逻辑中有一些问题

  • 首先,您使用的JSON字符串不是JSON格式的。GSON解析器无法解析相同的字符串
问题:

  • 照片的
    缩略图
    变量末尾还有一个逗号
  • public class Owner {
        /*
         * "ownerid" : "5633432",
         "name" : "Jackson",
         "year_own" : "1982-1985"
    
         * */
    
        private String ownerid;
        /**
         * Constructor for Owner.
         * @param ownerid
         * @param name
         * @param year_own <tt></tt>
         */
        public Owner(String ownerid, String name, String year_own) {
            super();
            this.ownerid = ownerid;
            this.name = name;
            this.year_own = year_own;
        }
        private String name;
        private String year_own;
        /**
         * Gets the ownerid.
         * 
         * @return <tt> the ownerid.</tt>
         */
        public String getOwnerid() {
            return ownerid;
        }
        /**
         * Sets the ownerid.
         *
         * @param ownerid <tt> the ownerid to set.</tt>
         */
        public void setOwnerid(String ownerid) {
            this.ownerid = ownerid;
        }
        /**
         * Gets the name.
         * 
         * @return <tt> the name.</tt>
         */
        public String getName() {
            return name;
        }
        /**
         * Sets the name.
         *
         * @param name <tt> the name to set.</tt>
         */
        public void setName(String name) {
            this.name = name;
        }
        /**
         * Gets the year_own.
         * 
         * @return <tt> the year_own.</tt>
         */
        public String getYear_own() {
            return year_own;
        }
        /**
         * Sets the year_own.
         *
         * @param year_own <tt> the year_own to set.</tt>
         */
        public void setYear_own(String year_own) {
            this.year_own = year_own;
        }
        /* (non-Javadoc)
         * @see java.lang.Object#toString()
         */
        @Override
        public String toString() {
            return "Owner [ownerid=" + ownerid + ", name=" + name + ", year_own="
                    + year_own + "]";
        }
    
    
    }
    
    public class CarDetail {
    
        private List<Photo> photos;
        private List<Info> info;
        private List<Owner> owners;
        /**
         * Gets the photos.
         * 
         * @return <tt> the photos.</tt>
         */
        public List<Photo> getPhotos() {
            return photos;
        }
        /**
         * Sets the photos.
         *
         * @param photos <tt> the photos to set.</tt>
         */
        public void setPhotos(List<Photo> photos) {
            this.photos = photos;
        }
        /**
         * Gets the info.
         * 
         * @return <tt> the info.</tt>
         */
        public List<Info> getInfo() {
            return info;
        }
        /**
         * Sets the info.
         *
         * @param info <tt> the info to set.</tt>
         */
        public void setInfo(List<Info> info) {
            this.info = info;
        }
        /**
         * Gets the owners.
         * 
         * @return <tt> the owners.</tt>
         */
        public List<Owner> getOwners() {
            return owners;
        }
        /**
         * Sets the owners.
         *
         * @param owners <tt> the owners to set.</tt>
         */
        public void setOwners(List<Owner> owners) {
            this.owners = owners;
        }
        /* (non-Javadoc)
         * @see java.lang.Object#toString()
         */
        @Override
        public String toString() {
            return "CarDetail [photos=" + photos + ", info=" + info + ", owners="
                    + owners + "]";
        }
    
    
    
    }
    
    String json = "{" + 
    " \"photos\" : [" + 
    "  {" + 
    "     \"thumbnail\" : \"http://www.myserver.com/01.jpg\"" + 
    "  }," + 
    "  {" + 
    "     \"thumbnail\" : \"http://www.myserver.com/02.jpg\"" + 
    "  }," + 
    "  {" + 
    "     \"thumbnail\" : \"http://www.myserver.com/03.jpg\"" + 
    "  }" + 
    "]," + 
    "\"info\" : [" + 
    "  {" + 
    "     \"id\" : \"1\"," + 
    "     \"description\" : \"My White Toyota\"," + 
    "     \"country\" : \"Japan\"," + 
    "     \"year\" : \"1982\"" + 
    "   }" + 
    " ]," + 
    "\"owners\" : [" + 
    "   {" + 
    "     \"ownerid\" : \"5633432\"," + 
    "     \"name\" : \"Jackson\"," + 
    "     \"year_own\" : \"1982-1985\"" + 
    "    }," + 
    "    {" + 
    "     \"ownerid\" : \"76832\"," + 
    "     \"name\" : \"Kurt\"," + 
    "     \"year_own\" : \"1986-1995\"" + 
    "    }," + 
    "    {" + 
    "     \"ownerid\" : \"236471\"," + 
    "     \"name\" : \"Alma\"," + 
    "     \"year_own\" : \"1999-2005\"" + 
    "    }" + 
    "   ]" + 
    "}";
    // parsing the JSON string to CardDetail object
     CarDetail detail=new Gson().fromJson(json,CarDetail.class);
    
     // retrieving data from the CardDetail object
     List<Photo> photos = detail.getPhotos();
     System.out.println("retrieving Photos");
     for (Photo photo : photos) {
        System.out.println(photo);
    }
    
     List<Info> info = detail.getInfo();
     System.out.println("retrieving Info");
     for (Info info2 : info) {
        System.out.println(info2);
    }
     System.out.println("retrieving Owners");
     List<Owner> owners = detail.getOwners();
     for (Owner owner : owners) {
        System.out.println(owner);
    }