非常に古いプロジェクトでJavaScriptで多次元配列を作成しましたが、今はJSから移り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に変換できますか?それはString[][] array = new String[5][5]
は「宣言の予期しない終了を」エラーが発生したと言うので
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];
:
注
ために私の携帯AIDEのバグに、私はこのような1次元配列を作成する必要があります、私は可能な場合、私が使用する形式で答えを好むだろう。
問題は、どのようにデータを保存するのかわかりません。このjavascriptコードでは、各要素は異なる属性を持つオブジェクトです。しかし、Javaでは、各オブジェクトが表すクラスを作成する必要があります。または、すべてをストリングに格納することもできます。あなたは何をするつもりですか? –
https://github.com/libgdx/libgdx/wiki/Reading-%26-writing-JSON – Xoppa