将此javascript数组转换为java
我在一个非常老的项目上用JavaScript创建了一个多维数组,现在我已经从JS转向Java。我正在开发一个LibGDX游戏,我需要这个java格式的数组,但我不知道如何使用将此javascript数组转换为java,javascript,java,libgdx,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"},
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
注意
由于我的移动助手有一个bug,我必须创建一个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];
publicstaticlist intro=newarraylist();
add(“带字符串的多个add语句”);
final CharSequence[]introItems=intro.toArray(新的CharSequence[intro.size()]);
退货项目[编号];
因为它说
String[][]array=new String[5][5]
有错误“声明的意外结束”,所以如果可能的话,我希望使用我使用的格式回答。如果您可以将数据稍微修改为纯JSON,可以这样做。我也不知道您是如何读取JS/JSON数据的,因此我假设您设法将其存储到解析代码中的字符串中:
jsonString
Merchant.Java
public class Merchant {
private String name;
private List<Item> items;
/* Getters/Setters */
}
public class Item {
private String name;
private String type;
private String slot;
private int cost;
/* Getters/Setters */
}
解析代码
//使用任何合适的方法读入字符串。
字符串jsonString=。。。;
//使用Jackson解析
ObjectMapper mapper=新的ObjectMapper();
List List=Arrays.asList(mapper.readValue(
jsonString,
商户[]类
);
如果希望将数据保留为数组,只需省略Arrays.asList()调用即可
如果无法将代码修改为JSON,可以使用来解析旧的JS代码。问题是,我们不知道如何存储数据。在这段javascript代码中,每个元素都是一个具有不同属性的对象。但是使用Java,您必须创建每个对象将表示的类。或者,您可以将所有内容都存储在Strin中你打算做什么?
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
);