将此javascript数组转换为java

时间:2016-04-27 17:50:37

标签: javascript java libgdx

我在一个非常古老的项目中用JavaScript创建了一个多维数组,现在我已经从JS转移并对Java感兴趣。我正在开发一个LibGDX游戏,我需要这个java格式的数组,但我不确定如何。

var merchants = [{
  name : 'Weapons Merchant',
  items : [
    {type: "weapon", cost: 250, name: "Claymore"},
    {type: "weapon", cost: 75,  name: "Dagger"},
    {type: "weapon", cost: 350, name: "Magic Staff"},
    {type: "weapon", cost: 150, name: "Sword"},
    {type: "weapon", cost: 125, name: "Bow"},
    {type: "weapon", cost: 125, name: "Crossbow"},
    {type: "weapon", cost: 5,   name: "Arrow"},
    {type: "weapon", cost: 15,  name: "Bolt"}
  ]
}, {
  name : 'Armor Merchant',
  items : [
    {type: "clothing", slot: "head",     name: "Helmet"},
    {type: "clothing", slot: "head",     name: "Hood"},
    {type: "clothing", slot: "chest",    name: "Chestplate"},
    {type: "clothing", slot: "chest",    name: "Tunic"},
    {type: "clothing", slot: "chest",    name: "Robe"},
    {type: "clothing", slot: "leggings", name: "Legplates"},
    {type: "clothing", slot: "leggings", name: "Leggings"},
    {type: "clothing", slot: "leggings", name: "Undergarments"},
    {type: "clothing", slot: "feet",     name: "Boots"},
    {type: "clothing", slot: "feet",     name: "Armored Boots"}
  ]
},  {
  name: 'Material Merchant',
  items:  [
    {type: "material", name: "Leather", cost: 25},
    {type: "material", name: "Iron", cost: 50},
    {type: "material", name: "Steel", cost: 75},
    {type: "material", name: "Mythril", cost: 100},
    {type: "material", name: "Dragonbone", cost: 200}
  ]
}];

如何将其转换为Java?

注意

由于我的移动AIDE存在错误,我必须创建一个这样的1d数组:

public static List<CharSequence> intro = new ArrayList<CharSequence>();
intro.add("multiple add statements with strings");
final CharSequence[] introItems = intro.toArray(new CharSequence[intro.size()]);
        return introItems[number];

因为它说String[][] array = new String[5][5]有错误&#34;意外结束声明&#34;所以我更喜欢我使用的格式的答案,如果可能的话。

1 个答案:

答案 0 :(得分:1)

如果您可以将数据稍微修改为纯JSON,Jackson可以执行此操作。我也不知道你如何阅读JS / JSON数据,所以我假设你设法将它存储在解析代码中的String中:

<强> jsonString

[{
  name : 'Weapons Merchant',
  items : [
    {type: "weapon", cost: 250, name: "Claymore"},
    {type: "weapon", cost: 75,  name: "Dagger"},
    {type: "weapon", cost: 350, name: "Magic Staff"},
    {type: "weapon", cost: 150, name: "Sword"},
    {type: "weapon", cost: 125, name: "Bow"},
    {type: "weapon", cost: 125, name: "Crossbow"},
    {type: "weapon", cost: 5,   name: "Arrow"},
    {type: "weapon", cost: 15,  name: "Bolt"}
  ]
}, {
  name : 'Armor Merchant',
  items : [
    {type: "clothing", slot: "head",     name: "Helmet"},
    {type: "clothing", slot: "head",     name: "Hood"},
    {type: "clothing", slot: "chest",    name: "Chestplate"},
    {type: "clothing", slot: "chest",    name: "Tunic"},
    {type: "clothing", slot: "chest",    name: "Robe"},
    {type: "clothing", slot: "leggings", name: "Legplates"},
    {type: "clothing", slot: "leggings", name: "Leggings"},
    {type: "clothing", slot: "leggings", name: "Undergarments"},
    {type: "clothing", slot: "feet",     name: "Boots"},
    {type: "clothing", slot: "feet",     name: "Armored Boots"}
  ]
},  {
  name: 'Material Merchant',
  items:  [
    {type: "material", name: "Leather", cost: 25},
    {type: "material", name: "Iron", cost: 50},
    {type: "material", name: "Steel", cost: 75},
    {type: "material", name: "Mythril", cost: 100},
    {type: "material", name: "Dragonbone", cost: 200}
  ]
}]

<强> Merchant.Java

public class Merchant {
    private String name;
    private List<Item> items;
    /* Getters/Setters */
}

<强> Item.Java

public class Item {
    private String name;
    private String type;
    private String slot;
    private int cost;
    /* Getters/Setters */
}

解析代码

// Read in the String using whatever means is appropriate.
String jsonString = ...;

// Parse using Jackson
ObjectMapper mapper = new ObjectMapper();
List<Merchant> list = Arrays.asList(mapper.readValue(
    jsonString,
    Merchant[].class
);

如果您希望数据保留为数组,请忽略 Arrays.asList()调用。

如果您无法将代码修改为JSON,则可以使用Rhino来解析旧的JS代码。