如何解决在对象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 举报,一经查实,本站将立刻删除。