1

我需要创建一个具有以下语法的数据结构:

[
 status: "success",
 statusDescription: "statusDescription"
 count: "5",
 states: [
  [stateId: "1", stateDescription: "1 description"],
  [stateId: "2", stateDescription: "2 description"],
  [stateId: "3", stateDescription: "3 description"],
  [stateId: "4", stateDescription: "4 description"],
  [stateId: "5", stateDescription: "5 description"]

 ]
]

我不确定如何在 Java 中做到这一点。我正在尝试 ArrayLists 和 Maps 的各种组合,但不太明白。

编辑:我实际上需要将此数据类型作为对 JAX-WS @WebMethod 调用的响应返回。创建一个单独的类,尤其是带有 List 元素的类会导致问题。

4

7 回答 7

5

Build your own class. Please.

class Foo {
  private final String status;
  private final String statusDescription;
  private final int count;
  private final List<State> states;
}

class State {
  private final int stateId;
  private final String stateDescription;
}
于 2012-09-27T18:45:07.637 回答
2
class Response {
   private boolean success;
   private String statusDescription; 
   private List<State> states;
}

class State {
   private int id;
   private String description;
}
于 2012-09-27T18:45:06.193 回答
0

我会这样写:

public class Whatever {
    private Status status;
    private String statusDescription;
    private int count;
    private List<Map<int,String>> states;
}    

或这个:

public class Whatever {
    private Status status;
    private String statusDescription;
    private int count;
    private List<State> states;
} 

其中 State 和 Status 是枚举。由你决定。我想状态有限

于 2012-09-27T18:55:11.423 回答
0

Define a class, with an array [or ArrayList] of states.

于 2012-09-27T18:44:43.477 回答
0

? Do your own class.

status, statusDescription are String properties of your class.

count is an integer property (or String if you want).

states is either an enum, a Map or a two dimensional array. Whatever fits.

于 2012-09-27T18:45:50.243 回答
0

真的很糟糕的设计,但正是你想要的:

public static void main(String[] args) {
    HashMap<String, Object> hashMap = new HashMap<String, Object>();
    hashMap.put("status", "success");
    hashMap.put("statusDescription", "statusDescription");
    hashMap.put("count", "5");
    List<Object> list = new ArrayList<Object>();
    hashMap.put("states", list);
    for (int i = 1; i < 5; i++) {
        HashMap<String, String> hashMapInternal = new HashMap<String, String>();
        hashMapInternal.put("stateId", String.valueOf(i));
        hashMapInternal.put("stateDescription", i + " description");
        list.add(hashMapInternal);
    }
}
于 2012-09-27T18:58:47.387 回答
0

为此,您可以创建一个类,例如

        class A

     {
       private int statusId;
        private String Description;
        private int count;
      }

现在根据您的要求创建一个数据结构,并将 A 类的对象添加到该数据结构中。

于 2012-09-27T18:47:38.857 回答