在对象Java中解析JSON对象

问题描述

正在尝试使用volley映射此json。但是我每次都在构造函数内部不断出现错误

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

所以我需要获取time_created对象。这是我的模特班; 公共类Neighbourhoodother实施可序列化{

    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;
        }
    }

}

这是我试图用凌空抽空的方法,但每次都会出现错误

  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);

}

构造函数中的错误表明它期望使用JSONObject而不是TimeCreated。什么是获取数据的最佳方法。我需要将date字符串传递给TextView

解决方法

from selenium import webdriver import time print("Googlen't Images") image_query = input("Enter path where image is: ") driver = webdriver.Chrome("C:\Program Files (x86)\chromedriver.exe") #driver = webdriver.Firefox(executable_path='/home/furas/bin/geckodriver') driver.get("https://images.google.com") cameraicon = driver.find_element_by_class_name("BwoPOe") cameraicon.click() time.sleep(1) # send word or url on first tab #box = driver.find_element_by_id("Ycyxxc") #box.send_keys(image_query) # send local file on second tab box = driver.find_element_by_id("awyMjb") box.send_keys(image_query) 返回Input.GetKey()而不是NeighbourhoodOther.TimeCreated timeCreated = neighOtherObject.getJSONObject("time_created")

尝试:

JSONObject
,

getJSONObject返回一个jsonobject,因此您应该将其转换为自定义类型。 有很多libray可以做到这一点,Gson就是这样。

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

    Gson gson= new Gson();

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...