【问题标题】:How to make JSON having loop inside lopp如何使 JSON 在循环中具有循环
【发布时间】:2014-06-11 06:11:36
【问题描述】:

我有下表 client_question 表

+----+------------+---------+-----+------+------+
| id | is_deleted | version | cid | pqid | qtid |
+----+------------+---------+-----+------+------+
|  1 |            |       0 |   1 |    1 |    1 |
|  2 |            |       0 |   1 |    2 |    4 |
|  3 |            |       0 |   1 |    2 |    4 |
+----+------------+---------+-----+------+------+

这是 Parent_question 表

+----+------------+---------+-----+------+
| id | is_deleted | version | pid | qid  |
+----+------------+---------+-----+------+
|  1 |            |       0 |   1 |    1 |
|  2 |            |       0 |   1 |    2 |
|  3 |            |       0 |   1 |    3 |
|  4 |            |       0 |   1 |    4 |
|  5 |            |       0 |   1 |    5 |
|  6 |            |       0 |   1 |    6 |
|  7 |            |       0 |   2 |    7 |
|  8 |            |       0 |   2 |    1 |
|  9 |            |       0 |   2 |    2 |
| 10 |            |       0 |   2 |    8 |
| 11 |            |       0 |   3 |    9 |
| 12 |            |       0 |   3 |    1 |
| 13 |            |       0 |   3 |   10 |
| 14 |            |       0 |   3 |   11 |
| 15 |            |       0 |   4 |   12 |
+----+------------+---------+-----+------+

这是 question_option

+----+------------+-----------+---------+
| id | is_deleted | name      | version |
+----+------------+-----------+---------+
|  1 |            | Excellent |       0 |
|  2 |            | Good      |       0 |
|  3 |            | Fair      |       0 |
|  4 |            | Poor      |       0 |
+----+------------+-----------+---------+

我想检索 JSON 并通过 ajax 发送到前端所以 我试过这种方式

public List<ClientQuestionOption> getSavedQuestionOptions(Long parentId,long clientId)
{
    Client client = (Client) entityManagerUtil.find(Client.class, clientId);
    List<ClientQuestionOption>  questionsList =     (List<ClientQuestionOption>)serviceClientDaoImpl.getSavedQuestionOptionsList(parentId,client);
    System.out.println("The size is nnnnnnnnnn "+questionsList.size());
    List optionsList =new ArrayList();

    for(int i=0;i<questionsList.size();i++)
    {
        //optionsList.add(questionsList.get(i).getCqid().getId());
        //optionsList.add(questionsList.get(i).getOid().getName());
        Map map=new HashMap();
        map.put("qid", questionsList.get(i).getCqid().getPqid().getQid().getId());
        map.put("name", questionsList.get(i).getOid().getName());
        optionsList.add(map);

    }
return optionsList;

}

我得到的JSON是这样的

[
{
name: "Excellent",
qid: 2
},
{
name: "Poor",
qid: 2
}
],

但我想要这样的 JSON

 {
      "options": [
        "Poot",
        "Excellent"
      ],
      "qid": 2
    },

谁能告诉我怎么做?

编辑

按照我正在做的方式制作 JSON

    JSONObject object=new JSONObject();
List optionslist=null;
optionslist=(List<ClientQuestionOption>)serviceClientServiceImpl.getSavedQuestionOptions(parentId , Long.valueOf(clientId) );
    object.accumulate("optionslist",optionslist );

    return object.toString();

【问题讨论】:

  • 从不使用原始类型开始(即不使用ListMap,而是使用List&lt;Map&lt;String, String&gt;&gt;,你会发现你的代码不再编译了,因为你不是返回正确的列表。
  • @JBNizet 你能告诉我怎么做吗?
  • 我刚做了。不要将optionsList 声明为List。将其声明为List&lt;Map&lt;String, String&gt;&gt;。不要将map 声明为Map。声明为Map&lt;String, String&gt;
  • @JBNizet 现在我明白了。谢谢

标签: java json spring hibernate


【解决方案1】:

首先,意识到函数返回的(ArrayList)根本不是JSON。这似乎发生在其他地方。您需要将 JsonObject() 替换为 JsonArray() 以将 [..] 更改为 '{..}`。

其次,要根据qid 进行分组,您可以编写一个以List&lt;ClientQuestionOption&gt; 作为输入和输出的函数。

【讨论】:

    【解决方案2】:

    您应该查看 google GSON 项目,您的代码如下所示:

    public class ClientQuestionOption {
        String name;
        int qid;
    
        public ClientQuestionOption() {
        }
    
        public ClientQuestionOption(String name, int qid) {
            this.name = name;
            this.qid = qid;
        }
    }
    

    然后这样使用:

    public static void main(String[] args) {
        List<ClientQuestionOption> questionOptions = Arrays.asList(
                new ClientQuestionOption("Excellent", 1),
                new ClientQuestionOption("Excellent", 2),
                new ClientQuestionOption("Poor", 3)
        );
    
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
    
        System.out.println(gson.toJson(questionOptions));
    }
    

    您只需要具有相同名称的字段(或声明限定符)的类,gson 将处理从/到 json 的转换。

    这是上面代码的输出:

    [
      {
        "name": "Excellent",
        "qid": 1
      },
      {
        "name": "Excellent",
        "qid": 2
      },
      {
        "name": "Poor",
        "qid": 3
      }
    ]
    

    【讨论】:

    • 感谢您的回答,现在我已经添加了 JSON 代码
    猜你喜欢
    • 1970-01-01
    • 2013-08-19
    • 2016-05-10
    • 2016-11-09
    • 2021-12-17
    • 2018-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多