英文:
Parsing JSON Object within an Object java
问题
I'm here to provide the translation you requested. Here's the translated code part:
正在尝试使用 `volley` 映射这个 JSON。但每次在我的构造函数内都会出现错误。
[
{
"id": 19,
"time_created": {
"date": "2018-09-18 09:24:34.000000",
"timezone_type": 3,
"timezone": "America/Chicago"
}
}
]
所以我需要提取 `time_created` 对象。这是我的模型类;
public class NeighbourhoodOther implements Serializable {
public int id;
public TimeCreated timeCreated;
public NeighbourhoodOther() {
}
public NeighbourhoodOther(int id, TimeCreated timeCreated ) {
this.id = id;
this.timeCreated = timeCreated;
}
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;
}
}
}
这是我尝试使用 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, timeCreated);
otherNeighbourHoodList.add(neighbourhoodOther);
}
构造函数内的错误说它期望一个 JSONObject 而不是 `TimeCreated`。最佳的数据提取方法是什么?我需要将 `date` 字符串传递给 `TextView`。
Please note that this translation is for your reference, and it's important to ensure that the code logic and structure are still correct in the context of your application.
英文:
Am trying to map this json with 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. This is my model class;
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.
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
. Whats the best way to fetch the data. I need to pass the date
String to a TextView
.
答案1
得分: 0
NeighbourhoodOther.TimeCreated timeCreated = neighOtherObject.getJSONObject("time_created")
返回一个 JSONObject
而不是 NeighbourhoodOther.TimeCreated
。
尝试:
JSONObject obj = neighOtherObject.getJSONObject("time_created");
NeighbourhoodOther.TimeCreated temp = new NeighbourhoodOther.TimeCreated()
temp.timezone = obj.getString("timezone");
... 填充 NeighbourhoodOther.TimeCreated 的成员
英文:
NeighbourhoodOther.TimeCreated timeCreated = neighOtherObject.getJSONObject("time_created")
returns a JSONObject
not 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
答案2
得分: 0
getJSONObject返回一个 JSONObject,因此您应该将其转换为自定义类型。
有许多库可以做到这一点,Gson
是其中之一。
import com.google.gson.Gson;
// 其他代码
Gson gson = new Gson();
TimeCreated obj = gson.fromJson(neighOtherObject.getJSONObject("time_created").toString(), TimeCreated.class);
英文:
getJSONObject return a jsonobject, so you should cast it to custom type.
There are lots of libray to do this, Gson
is the one.
import com.google.gson.Gson;
//other stuffs
Gson gson= new Gson();
TimeCreated obj = gson.fromJson(neighOtherObject.getJSONObject("time_created").toString(), TimeCreated.class);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论