从JSON字符串中获取json对象

时间:2017-06-02 11:16:52

标签: android json listview parsing gson

我是Android新手,我正在开发具有服务器端功能的应用程序。我收到了JSON格式的回复。

我的回复显示为此图片。

我知道如何使用Volley解析json但我不知道使用GSON解析热点。我的应用程序的先前代码是由其他人编写的。现在我必须完成这段代码。但我不知道他是如何从JSON字符串中获取数据的。

我需要在不同活动中使用JSON数组。数组响应:

image

以下是我的代码的一些快照。

活动一的适配器代码

  topicListAdapter = new TopicListAdapter(TopicActivity.this,

                myCourseListMain.  getCourseArrayList().get(Integer.parseInt(course_position)).
                getTopicListMain().getTopicDetailsArrayList(), flag);


        listAlltopics.setAdapter(topicListAdapter);

我获得了主题列表

这是第二个活动列表适配器的代码

 lessionListAdapter = new LessionListAdapter(LessionActivity.this,
                myCourseListMain.  getCourseArrayList().      get(Integer.parseInt(course_position)).
                getTopicListMain().getTopicDetailsArrayList().get(Integer.parseInt(topic_position)).getLessionArrayList(), flag);

通过这段代码,我在第二个活动中得到了一些内容

现在我想在第三项活动中获得sublession数组,但我不知道如何获得它。 这是我试过的

 lessionListAdapter = new DummyAdapter(DummyTopicList.this,
                myCourseListMain     .  getCourseArrayList().         get(Integer.parseInt(course_position)).
                getTopicListMain()   .  getTopicDetailsArrayList()   .get(Integer.parseInt(topic_position)).
                getLessionLIstMain() .getLessionLIstDetailArrayList().get(Integer.parseInt(lession_position)). , flag);

        listAlllessions.setAdapter(lessionListAdapter);

以下是一些有助于您理解的其他课程

public class MyCourseListMain {

@SerializedName("data")
private ArrayList<Course> courseArrayList;

public ArrayList<Course> getCourseArrayList() {
    return courseArrayList;
}

public void setCourseArrayList(ArrayList<Course> courseArrayList) {
    this.courseArrayList = courseArrayList;    
}
}

课程

public class Course {


@SerializedName("img")
private String img;

@SerializedName("title")
private String title;

@SerializedName("institute_id")
private String institute_id;

@SerializedName("institute_name")
private String institute_name;

@SerializedName("expired")
private String expired;

@SerializedName("status")
private String status;

@SerializedName("subscribe_box")
private String subscribe_box;

@SerializedName("expire_on")
private String expire_on;

@SerializedName("item_id")
private String item_id;

@SerializedName("rated")
private String rated;

private TopicListMain topicListMain;



public String getRated() {
    return rated;
}

public void setRated(String rated) {
    this.rated = rated;
}

public String getImg() {
    return img;
}

public void setImg(String img) {
    this.img = img;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getInstitute_id() {
    return institute_id;
}

public void setInstitute_id(String institute_id) {
    this.institute_id = institute_id;
}

public String getInstitute_name() {
    return institute_name;
}

public void setInstitute_name(String institute_name) {
    this.institute_name = institute_name;
}

public String getExpired() {
    return expired;
}

public void setExpired(String expired) {
    this.expired = expired;
}

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

public String getSubscribe_box() {
    return subscribe_box;
}

public void setSubscribe_box(String subscribe_box) {
    this.subscribe_box = subscribe_box;
}

public String getExpire_on() {
    return expire_on;
}

public void setExpire_on(String expire_on) {
    this.expire_on = expire_on;
}

public String getItem_id() {
    return item_id;
}

public void setItem_id(String item_id) {
    this.item_id = item_id;
}

public TopicListMain getTopicListMain() {
    return topicListMain;
}

public void setTopicListMain(TopicListMain topicListMain) {
    this.topicListMain = topicListMain;   }  }

topiclist_main类

public class TopicListMain {


@SerializedName("data")
private ArrayList<TopicDetails> topicDetailsArrayList;

public ArrayList<TopicDetails> getTopicDetailsArrayList() {
    return topicDetailsArrayList;
}



public void setTopicDetailsArrayList(ArrayList<TopicDetails> topicDetailsArrayList) {

    this.topicDetailsArrayList = topicDetailsArrayList;   }}

主题详情课程

public class TopicDetails 
{


@SerializedName("topic_id")
private String topic_id;

@SerializedName("title")
private String title;

@SerializedName("locked")
private String locked;

@SerializedName("lessons")
private ArrayList<Lession> lessionArrayList;


private LessionLIstMain lessionLIstMain;

public LessionLIstMain getLessionLIstMain() {
    return lessionLIstMain;
}

public void setLessionLIstMain(LessionLIstMain lessionLIstMain) {
    this.lessionLIstMain = lessionLIstMain;
}

public String getTopic_id() {
    return topic_id;
}

public void setTopic_id(String topic_id) {
    this.topic_id = topic_id;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getLocked() {
    return locked;
}

public void setLocked(String locked) {
    this.locked = locked;
}

public ArrayList<Lession> getLessionArrayList() {
    return lessionArrayList;
}



public void setLessionArrayList(ArrayList<Lession> lessionArrayList) {
    this.lessionArrayList = lessionArrayList;   }}

2 个答案:

答案 0 :(得分:0)

https://github.com/google/gson 使你的对象与你得到的数据具有相同的结构。

YourObject val = new Gson().fromJson(new String(YourString.getBytes("ISO-8859-1"),
"UTF-8"), YourObject.class);

答案 1 :(得分:0)

最后我通过以下代码得到了我的解决方案。

   lessionListAdapter = new DummyAdapter(DummyTopicList.this,
                myCourseListMain     .  getCourseArrayList().         get(Integer.parseInt(course_position)).
                getTopicListMain()   .  getTopicDetailsArrayList()   .get(Integer.parseInt(topic_position)).
                getLessionArrayList().get(Integer.parseInt((lession_position))).getLessionLIstDetailArrayList() , flag);
              listAlllessions.setAdapter(lessionListAdapter);

我还做了一些类来处理json数组。

public class SubLessionDetail {
@SerializedName("lesson_id")
private String lession_id;

@SerializedName("title")
private String title;

@SerializedName("locked")
private String locked;


public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getLocked() {
    return locked;
}

public void setLocked(String locked) {
    this.locked = locked;
}

public String getLession_id() {

    return lession_id;
}

public void setLession_id(String lession_id) {
    this.lession_id = lession_id;
}
}