我使用Retrofit和简单的对象,但是这个当前的项目让我很头疼。我正在尝试解析高尔夫球场的信息。
如果您的api响应如下所示,那么最佳方法是什么:
[
{
"course_id":"1",
"name":"Prompton State Park",
"city":"Honesdale",
"state":"PA",
"country":"United States",
"reviews":"9",
"rating":"3.72",
"tee_1_clr":"FFFF00",
"tee_2_clr":"FF0000",
"tee_3_clr":"",
"tee_4_clr":"",
"dgcr_url":"https:\/\/www.dgcoursereview.com\/course.php?id=1",
"dgcr_mobile_url":"https:\/\/www.dgcoursereview.com\/mobile\/course.php?
id=1",
"rating_img":"https:\/\/www.dgcoursereview.com\/images\/rating\/discs_3.5.png",
"rating_img_small":"https:\/\/www.dgcoursereview.com\/images\/rating\/discs_sm_3.5.png"
},
{
"hole_num":"1",
"hole_lbl":"1",
"tee_1_len":"324",
"tee_2_len":"436",
"tee_3_len":"0",
"tee_4_len":"0",
"tee_1_par":"3",
"tee_2_par":"3",
"tee_3_par":"0",
"tee_4_par":"0"
},
{
"hole_num":"2",
"hole_lbl":"2",
"tee_1_len":"282",
"tee_2_len":"390",
"tee_3_len":"0",
"tee_4_len":"0",
"tee_1_par":"3",
"tee_2_par":"4",
"tee_3_par":"0",
"tee_4_par":"0"
},
{
"hole_num":"3",
"hole_lbl":"3",
"tee_1_len":"352",
"tee_2_len":"376",
"tee_3_len":"0",
"tee_4_len":"0",
"tee_1_par":"3",
"tee_2_par":"3",
"tee_3_par":"0",
"tee_4_par":"0"
},
{
"hole_num":"4",
"hole_lbl":"4",
"tee_1_len":"258",
"tee_2_len":"319",
"tee_3_len":"0",
"tee_4_len":"0",
"tee_1_par":"3",
"tee_2_par":"3",
"tee_3_par":"0",
"tee_4_par":"0"
},
{
"hole_num":"5",
"hole_lbl":"5",
"tee_1_len":"188",
"tee_2_len":"262",
"tee_3_len":"0",
"tee_4_len":"0",
"tee_1_par":"3",
"tee_2_par":"3",
"tee_3_par":"0",
"tee_4_par":"0"
},
{
"hole_num":"6",
"hole_lbl":"6",
"tee_1_len":"245",
"tee_2_len":"364",
"tee_3_len":"0",
"tee_4_len":"0",
"tee_1_par":"3",
"tee_2_par":"3",
"tee_3_par":"0",
"tee_4_par":"0"
},
{
"hole_num":"7",
"hole_lbl":"7",
"tee_1_len":"239",
"tee_2_len":"282",
"tee_3_len":"0",
"tee_4_len":"0",
"tee_1_par":"3",
"tee_2_par":"3",
"tee_3_par":"0",
"tee_4_par":"0"
},
{
"hole_num":"8",
"hole_lbl":"8",
"tee_1_len":"409",
"tee_2_len":"508",
"tee_3_len":"0",
"tee_4_len":"0",
"tee_1_par":"3",
"tee_2_par":"4",
"tee_3_par":"0",
"tee_4_par":"0"
},
{
"hole_num":"9",
"hole_lbl":"9",
"tee_1_len":"255",
"tee_2_len":"301",
"tee_3_len":"0",
"tee_4_len":"0",
"tee_1_par":"3",
"tee_2_par":"3",
"tee_3_par":"0",
"tee_4_par":"0"
},
{
"hole_num":"10",
"hole_lbl":"10",
"tee_1_len":"446",
"tee_2_len":"555",
"tee_3_len":"0",
"tee_4_len":"0",
"tee_1_par":"3",
"tee_2_par":"3",
"tee_3_par":"0",
"tee_4_par":"0"
},
{
"hole_num":"11",
"hole_lbl":"11",
"tee_1_len":"416",
"tee_2_len":"750",
"tee_3_len":"0",
"tee_4_len":"0",
"tee_1_par":"3",
"tee_2_par":"4",
"tee_3_par":"0",
"tee_4_par":"0"
},
{
"hole_num":"12",
"hole_lbl":"12",
"tee_1_len":"266",
"tee_2_len":"393",
"tee_3_len":"0",
"tee_4_len":"0",
"tee_1_par":"3",
"tee_2_par":"3",
"tee_3_par":"0",
"tee_4_par":"0"
},
{
"hole_num":"13",
"hole_lbl":"13",
"tee_1_len":"603",
"tee_2_len":"956",
"tee_3_len":"0",
"tee_4_len":"0",
"tee_1_par":"3",
"tee_2_par":"5",
"tee_3_par":"0",
"tee_4_par":"0"
},
{
"hole_num":"14",
"hole_lbl":"14",
"tee_1_len":"345",
"tee_2_len":"445",
"tee_3_len":"0",
"tee_4_len":"0",
"tee_1_par":"3",
"tee_2_par":"3",
"tee_3_par":"0",
"tee_4_par":"0"
},
{
"hole_num":"15",
"hole_lbl":"15",
"tee_1_len":"363",
"tee_2_len":"556",
"tee_3_len":"0",
"tee_4_len":"0",
"tee_1_par":"3",
"tee_2_par":"3",
"tee_3_par":"0",
"tee_4_par":"0"
},
{
"hole_num":"16",
"hole_lbl":"16",
"tee_1_len":"277",
"tee_2_len":"383",
"tee_3_len":"0",
"tee_4_len":"0",
"tee_1_par":"3",
"tee_2_par":"3",
"tee_3_par":"0",
"tee_4_par":"0"
},
{
"hole_num":"17",
"hole_lbl":"17",
"tee_1_len":"288",
"tee_2_len":"553",
"tee_3_len":"0",
"tee_4_len":"0",
"tee_1_par":"3",
"tee_2_par":"4",
"tee_3_par":"0",
"tee_4_par":"0"
},
{
"hole_num":"18",
"hole_lbl":"18",
"tee_1_len":"307",
"tee_2_len":"372",
"tee_3_len":"0",
"tee_4_len":"0",
"tee_1_par":"3",
"tee_2_par":"3",
"tee_3_par":"0",
"tee_4_par":"0"
}
]
目前我的对象是
private String name;
private String city;
private String state;
private int holes;
private int courseId;
private String dgcrMobileLink;
private int reviewCount;
private double rating;
private double latitude;
private double longitude;
private String teeOneColor;
private Boolean teeOneExists;
public ArrayList<Hole> teeOneHoles;
private String teeTwoColor;
private Boolean teeTwoExists;
private ArrayList<Hole> teeTwoHoles;
private String teeThreeColor;
private Boolean teeThreeExists;
private ArrayList<Hole> teeThreeHoles;
private String teeFourColor;
private Boolean teeFourExists;
private ArrayList<Hole> teeFourHoles;
在应用程序的第一次运行中,我使用自定义加载器来解析JSON。有没有办法告诉改造如何处理JSON格式..?或者有更好的方法来解决这个问题吗?
答案 0 :(得分:1)
同一个数组中有2个不同的对象。因此,为了简单起见,我只需要一个包含以下内容的模型类:
public class GolfInfo {
private int course_id;
private String name;
private String city;
private String state;
private String country;
private int reviews;
private float rating;
private String tee_1_clr;
private String tee_2_clr;
private String tee_3_clr;
private String tee_4_clr;
private String dgcr_url;
private String dgcr_mobile_url;
private String rating_img;
private String rating_img_small;
private int hole_num;
private int hole_lbl;
private int tee_1_len;
private int tee_2_len;
private int tee_3_len;
private int tee_4_len;
private int tee_1_par;
private int tee_2_par;
private int tee_3_par;
private int tee_4_par;
// getters
}
我假设您使用Gson反序列化您的数据。现在你可以简单地做通常的
Call<List<GolfInfo>> getGolfInfo();
当您获得数据时,您确定列表的第一个元素将包含course_id,name,city等。其余元素将包含hole_num,tee_1_len等。
答案 1 :(得分:-1)
你基本上有两种数据类型 - 一个过程然后一个洞。球场有一系列的洞。您可以使用GSON反序列化/序列化此数据。
您可以使用此类工具生成模型类