在对象Java中解析JSON对象

如何解决在对象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);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?