【问题标题】:How can i create an array that contains another array?如何创建包含另一个数组的数组?
【发布时间】:2019-09-03 12:34:07
【问题描述】:

我需要保存一些从列表视图中获取的值,我的问题是如何执行此实现来保存我的值? 我需要这样做。

[
    {
        "id_question": "my value here",
        "answers": [
            {
                "my_answer":"value for this answer",
                "id_answer":"my value here"
            },
            {
                "my_answer":"value for this answer",
                "id_answer":"my value here"
            }
        ]
    }
]

最近在用

List<Map<String, String>[]> listOfMaps = new ArrayList<Map<String, String>[]>();

【问题讨论】:

  • 您可以保存在共享首选项中。
  • 对不起,我最近编辑了我的问题,我不想保存我想在 android 中创建该结构。
  • 你为什么要使用它?那是一个包含地图数组的列表。您可能正在寻找 List&lt;Map&lt;String, Object&gt;&gt; 并转换为 List&lt;Map&lt;String, String&gt;&gt;

标签: android arrays list


【解决方案1】:

Answer.java

public class Answer {
     private String myAnswer;
     private String idAnswer;
     //constructor, setters and getters
}

Question.java

public class Question {
     private String question;
     private List<Answer> answerList;
     //constructor, setters and getters
}

在你现在的班级

private List<Question> questionList;

如果我将您问题中的“保存”一词理解为保存到本地数据库,那么这些类可以直接转换为领域类。

附:获得这种数据结构的简单方法是坚持基础。也就是说,对象是实时实体的表示。

【讨论】:

  • 嗨 Mohammed 谢谢,我理解那个例子,但我想发送一个类似我的例子的结构,因为我需要将它发送到 php,在 php 中我需要接收这样的结构,我更新了我的问题,因为我不想保存它,我想创建一个类似的结构,抱歉我的错误解释
  • @Hugo Ramos 您可以使用 Amit 提到的 GSON 将上述数据结构直接转换为 json。或者您可以使用 JsonObject 和 Jsonarray 的组合直接创建 json 对象
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-09
  • 1970-01-01
  • 2020-02-26
  • 1970-01-01
相关资源
最近更新 更多