简体   繁体   English

在 Object Z93F725A07423321C889F644BZ 内解析 JSON Object

[英]Parsing JSON Object within an Object java

Am trying to map this json with volley .我正在尝试 map 这个 json volley But I keep getting an error inside my constructor each time.但是我每次都在构造函数中遇到错误。

[
    {
        "id": 19,
        "time_created": {
            "date": "2018-09-18 09:24:34.000000",
            "timezone_type": 3,
            "timezone": "America/Chicago"
        },
]

So I need to fetch the time_created object.所以我需要获取 time_created object。 This is my model class;这是我的 model class; public class NeighbourhoodOther implements Serializable{ public class NeighbourhoodOther implements Serializable{

    public int id;
    public TimeCreated timeCreated;
 public NeighbourhoodOther(){

    }
    public NeighbourhoodOther(int id, TimeCreated timeCreated ) {
        this.id = id;
        this.timeCreated= time_created;
       }

    public TimeCreated getTimeCreated() {
        return timeCreated;
    }

    public void setTimeCreated(TimeCreated timeCreated) {
        this.timeCreated = timeCreated;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
    public static class TimeCreated {
        public String timezone;
        public int timezoneType;
        public String date;

        public String getTimezone() {
            return timezone;
        }

        public void setTimezone(String timezone) {
            this.timezone = timezone;
        }

        public int getTimezoneType() {
            return timezoneType;
        }

        public void setTimezoneType(int timezoneType) {
            this.timezoneType = timezoneType;
        }

        public String getDate() {
            return date;
        }

        public void setDate(String date) {
            this.date = date;
        }
    }

}

This is the way am trying to fetch with volley but I get an error each time.这是我尝试使用 volley 获取的方式,但每次都会出错。

  for (int i = 0; i < neighOther.length(); i++) {


                            JSONObject neighOtherObject = neighOther.getJSONObject(i);
                                int id = neighOtherObject.getInt("id");
                          
                           
                           NeighbourhoodOther.TimeCreated timeCreated = neighOtherObject.getJSONObject("time_created");
                              

                                NeighbourhoodOther neighbourhoodOther = new NeighbourhoodOther(id, time_created);
                               otherNeighbourHoodList.add(neighbourhoodOther);

}

The error inside my constructor says its expecting a JSONObject instead of TimeCreated .我的构造函数中的错误说它需要一个 JSONObject 而不是TimeCreated Whats the best way to fetch the data.获取数据的最佳方法是什么。 I need to pass the date String to a TextView .我需要将date字符串传递给TextView

NeighbourhoodOther.TimeCreated timeCreated = neighOtherObject.getJSONObject("time_created") returns a JSONObject not NeighbourhoodOther.TimeCreated NeighbourhoodOther.TimeCreated timeCreated = neighOtherObject.getJSONObject("time_created")返回JSONObject而不是NeighbourhoodOther.TimeCreated

try:尝试:

JSONObject obj = neighOtherObject.getJSONObject("time_created");
NeighbourhoodOther.TimeCreated temp = new NeighbourhoodOther.TimeCreated()
temp.timezone = obj.getString("timezone");
... fill in NeighbourhoodOther.TimeCreated members

getJSONObject return a jsonobject, so you should cast it to custom type. getJSONObject返回一个 jsonobject,因此您应该将其转换为自定义类型。 There are lots of libray to do this, Gson is the one.有很多库可以做到这一点, Gson就是其中之一。

    import com.google.gson.Gson; 
    
    //other stuffs

    Gson gson= new Gson();

    TimeCreated obj = gson.fromJson(neighOtherObject.getJSONObject("time_created").toString(), TimeCreated.class);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM