Javascript 如何在java中构造类似json的body对象?

Javascript 如何在java中构造类似json的body对象?,javascript,java,arrays,Javascript,Java,Arrays,我是java编程的初学者,主要是javascript开发人员。我有一个js数组 [ { "key1": 0, "key2": 1, "key3": "string" }, { "key1": 1, "key2": 2, "key3": "string2" } ] 如何在Java中构造相同的结构 首先,您必须创建一个包含变量key1、key2和key3的Java类。 然后使用Jackson库将Json解析为Java对象。从我所看到的情况

我是java编程的初学者,主要是javascript开发人员。我有一个js数组

[
  {
    "key1": 0,
    "key2": 1,
    "key3": "string"
  },
{
    "key1": 1,
    "key2": 2,
    "key3": "string2"
  }
]

如何在Java中构造相同的结构

首先,您必须创建一个包含变量key1、key2和key3的Java类。
然后使用Jackson库将Json解析为Java对象。

从我所看到的情况来看,如果您不关心类型,只需使用映射列表即可创建您想要实现的目标(不推荐)

List myMap=new ArrayList();
但是如果你真的想使用Java,你需要学习面向对象编程。您将需要创建一个具有强类型属性的类。
创建类后,您可以创建一个数组,并用所述类(对象)的实例填充该数组。

选项1:地图列表。不是类型安全的,因为我们不声明确切的值类型

List<Map<String, ?>> list = List.of(
    Map.of(
        "key1", 0,
        "key2", 1,
        "key3", "string"
    ),
    Map.of(
        "key1", 1,
        "key2", 2,
        "key3", "string2"
    )
);

列表与阵列当然可以任意选择,我只是想在那里同时显示这两个选项。

根据需要,您需要按照以下步骤进行操作:

1) 使用成员(键1、键2、键3)创建一个

2) 创建该类的对象并将其添加到列表中。(正如您在评论中提到的,您必须迭代它,最好使用。)

第1步:

public class Data {

  private int key1;

  private int key2;

  private String key3;

  public Data(final int key1, final int key2, final String key3) {
    super();
    this.key1 = key1;
    this.key2 = key2;
    this.key3 = key3;
  }

  /**
   * @return the key1
   */
  public int getKey1() {
    return key1;
  }

  /**
   * @return the key2
   */
  public int getKey2() {
    return key2;
  }

  /**
   * @return the key3
   */
  public String getKey3() {
    return key3;
  }

  /**
   * @param key1
   *          the key1 to set
   */
  public void setKey1(final int key1) {
    this.key1 = key1;
  }

  /**
   * @param key2
   *          the key2 to set
   */
  public void setKey2(final int key2) {
    this.key2 = key2;
  }

  /**
   * @param key3
   *          the key3 to set
   */
  public void setKey3(final String key3) {
    this.key3 = key3;
  }

}
public class Test {

  public static void main(final String[] args) {

    //Using List
    final List<Data> myDataList = new ArrayList<Data>();
    myDataList.add(new Data(0, 1, "string"));
    myDataList.add(new Data(1, 2, "string2"));

    // Or

    //Using Array
    final Data[] myData = {new Data(0, 1, "string"), new Data(1, 2, "string2")};


  }

}
第二步:

public class Data {

  private int key1;

  private int key2;

  private String key3;

  public Data(final int key1, final int key2, final String key3) {
    super();
    this.key1 = key1;
    this.key2 = key2;
    this.key3 = key3;
  }

  /**
   * @return the key1
   */
  public int getKey1() {
    return key1;
  }

  /**
   * @return the key2
   */
  public int getKey2() {
    return key2;
  }

  /**
   * @return the key3
   */
  public String getKey3() {
    return key3;
  }

  /**
   * @param key1
   *          the key1 to set
   */
  public void setKey1(final int key1) {
    this.key1 = key1;
  }

  /**
   * @param key2
   *          the key2 to set
   */
  public void setKey2(final int key2) {
    this.key2 = key2;
  }

  /**
   * @param key3
   *          the key3 to set
   */
  public void setKey3(final String key3) {
    this.key3 = key3;
  }

}
public class Test {

  public static void main(final String[] args) {

    //Using List
    final List<Data> myDataList = new ArrayList<Data>();
    myDataList.add(new Data(0, 1, "string"));
    myDataList.add(new Data(1, 2, "string2"));

    // Or

    //Using Array
    final Data[] myData = {new Data(0, 1, "string"), new Data(1, 2, "string2")};


  }

}
公共类测试{
公共静态void main(最终字符串[]args){
//使用列表
最终列表myDataList=new ArrayList();
添加(新数据(0,1,“字符串”);
添加(新数据(1,2,“string2”);
//或
//使用数组
最终数据[]myData={新数据(0,1,“字符串”),新数据(1,2,“字符串2”)};
}
}

您必须使用key1、key2和key3以及setter/getter创建一个Java类。在此之后,您可以使用maven的google gson(如果您使用maven项目)或Json for java()。

如何处理构造的结构?也许可以查看自定义类和结构,如Array、ArrayList…请搜索如何使用类,JavaI中的对象和数组将在其上迭代并验证。@可能此链接不会帮助您
映射
,因为有些值是数字(
整数
)。OP不会要求解析JSON。OP正在显示JavaScript代码,并询问等效的Java代码是什么。“使用数组”是错误的,您将两者分配到相同的索引。如果使用数组初始值设定项,会更简单,更接近JS版本,它也会自动修复错误。@Andreas:很抱歉数组索引的输入错误。是,使用数组初始值设定项是有意义的。感谢您的投入。:)
public class Test {

  public static void main(final String[] args) {

    //Using List
    final List<Data> myDataList = new ArrayList<Data>();
    myDataList.add(new Data(0, 1, "string"));
    myDataList.add(new Data(1, 2, "string2"));

    // Or

    //Using Array
    final Data[] myData = {new Data(0, 1, "string"), new Data(1, 2, "string2")};


  }

}