解析 Java 中的嵌套 JSON 对象

huangapple go评论116阅读模式
英文:

Parsing JSON Object within an Object java

问题

I'm here to provide the translation you requested. Here's the translated code part:

  1. 正在尝试使用 `volley` 映射这个 JSON。但每次在我的构造函数内都会出现错误。
  2. [
  3. {
  4. "id": 19,
  5. "time_created": {
  6. "date": "2018-09-18 09:24:34.000000",
  7. "timezone_type": 3,
  8. "timezone": "America/Chicago"
  9. }
  10. }
  11. ]
  12. 所以我需要提取 `time_created` 对象。这是我的模型类;
  13. public class NeighbourhoodOther implements Serializable {
  14. public int id;
  15. public TimeCreated timeCreated;
  16. public NeighbourhoodOther() {
  17. }
  18. public NeighbourhoodOther(int id, TimeCreated timeCreated ) {
  19. this.id = id;
  20. this.timeCreated = timeCreated;
  21. }
  22. public TimeCreated getTimeCreated() {
  23. return timeCreated;
  24. }
  25. public void setTimeCreated(TimeCreated timeCreated) {
  26. this.timeCreated = timeCreated;
  27. }
  28. public int getId() {
  29. return id;
  30. }
  31. public void setId(int id) {
  32. this.id = id;
  33. }
  34. public static class TimeCreated {
  35. public String timezone;
  36. public int timezoneType;
  37. public String date;
  38. public String getTimezone() {
  39. return timezone;
  40. }
  41. public void setTimezone(String timezone) {
  42. this.timezone = timezone;
  43. }
  44. public int getTimezoneType() {
  45. return timezoneType;
  46. }
  47. public void setTimezoneType(int timezoneType) {
  48. this.timezoneType = timezoneType;
  49. }
  50. public String getDate() {
  51. return date;
  52. }
  53. public void setDate(String date) {
  54. this.date = date;
  55. }
  56. }
  57. }
  58. 这是我尝试使用 Volley 提取的方式,但每次都出错。
  59. for (int i = 0; i < neighOther.length(); i++) {
  60. JSONObject neighOtherObject = neighOther.getJSONObject(i);
  61. int id = neighOtherObject.getInt("id");
  62. NeighbourhoodOther.TimeCreated timeCreated = neighOtherObject.getJSONObject("time_created");
  63. NeighbourhoodOther neighbourhoodOther = new NeighbourhoodOther(id, timeCreated);
  64. otherNeighbourHoodList.add(neighbourhoodOther);
  65. }
  66. 构造函数内的错误说它期望一个 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.

  1. [
  2. {
  3. "id": 19,
  4. "time_created": {
  5. "date": "2018-09-18 09:24:34.000000",
  6. "timezone_type": 3,
  7. "timezone": "America/Chicago"
  8. },
  9. ]

So I need to fetch the time_created object. This is my model class;
public class NeighbourhoodOther implements Serializable{

  1. public int id;
  2. public TimeCreated timeCreated;
  3. public NeighbourhoodOther(){
  4. }
  5. public NeighbourhoodOther(int id, TimeCreated timeCreated ) {
  6. this.id = id;
  7. this.timeCreated= time_created;
  8. }
  9. public TimeCreated getTimeCreated() {
  10. return timeCreated;
  11. }
  12. public void setTimeCreated(TimeCreated timeCreated) {
  13. this.timeCreated = timeCreated;
  14. }
  15. public int getId() {
  16. return id;
  17. }
  18. public void setId(int id) {
  19. this.id = id;
  20. }
  21. public static class TimeCreated {
  22. public String timezone;
  23. public int timezoneType;
  24. public String date;
  25. public String getTimezone() {
  26. return timezone;
  27. }
  28. public void setTimezone(String timezone) {
  29. this.timezone = timezone;
  30. }
  31. public int getTimezoneType() {
  32. return timezoneType;
  33. }
  34. public void setTimezoneType(int timezoneType) {
  35. this.timezoneType = timezoneType;
  36. }
  37. public String getDate() {
  38. return date;
  39. }
  40. public void setDate(String date) {
  41. this.date = date;
  42. }
  43. }
  44. }

This is the way am trying to fetch with volley but I get an error each time.

  1. for (int i = 0; i < neighOther.length(); i++) {
  2. JSONObject neighOtherObject = neighOther.getJSONObject(i);
  3. int id = neighOtherObject.getInt("id");
  4. NeighbourhoodOther.TimeCreated timeCreated = neighOtherObject.getJSONObject("time_created");
  5. NeighbourhoodOther neighbourhoodOther = new NeighbourhoodOther(id, time_created);
  6. otherNeighbourHoodList.add(neighbourhoodOther);
  7. }

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

尝试:

  1. JSONObject obj = neighOtherObject.getJSONObject("time_created");
  2. NeighbourhoodOther.TimeCreated temp = new NeighbourhoodOther.TimeCreated()
  3. temp.timezone = obj.getString("timezone");
  4. ... 填充 NeighbourhoodOther.TimeCreated 的成员
英文:

NeighbourhoodOther.TimeCreated timeCreated = neighOtherObject.getJSONObject("time_created") returns a JSONObject not NeighbourhoodOther.TimeCreated

try:

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

答案2

得分: 0

getJSONObject返回一个 JSONObject,因此您应该将其转换为自定义类型。
有许多库可以做到这一点,Gson 是其中之一。

  1. import com.google.gson.Gson;
  2. // 其他代码
  3. Gson gson = new Gson();
  4. 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.

  1. import com.google.gson.Gson;
  2. //other stuffs
  3. Gson gson= new Gson();
  4. TimeCreated obj = gson.fromJson(neighOtherObject.getJSONObject("time_created").toString(), TimeCreated.class);

huangapple
  • 本文由 发表于 2020年8月12日 08:48:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63368213.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定