-2
我在一个非常旧的项目中使用JavaScript创建了一个多维数组,现在我已经从JS转向了Java,并对Java感兴趣。我正在开发一个LibGDX游戏,我需要这种数组的java格式,但我不知道如何。将此javascript数组转换为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的错误,我要创建一维数组是这样的:
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]
有错误“申报意外结束” ,所以如果可能的话,我更喜欢用我使用的格式的答案。
问题是,我们不知道你将如何存储数据。在这个JavaScript代码中,每个元素都是具有不同属性的对象。但对于Java,您必须创建每个对象所代表的类。或者,您可以将所有内容存储在字符串中。你打算做什么? –
https://github.com/libgdx/libgdx/wiki/Reading-%26-writing-JSON – Xoppa