2016-04-27 7 views
-2

非常に古いプロジェクトで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次元配列を作成する必要があります、私は可能な場合、私が使用する形式で答えを好むだろう。

+0

問題は、どのようにデータを保存するのかわかりません。このjavascriptコードでは、各要素は異なる属性を持つオブジェクトです。しかし、Javaでは、各オブジェクトが表すクラスを作成する必要があります。または、すべてをストリングに格納することもできます。あなたは何をするつもりですか? –

+0

https://github.com/libgdx/libgdx/wiki/Reading-%26-writing-JSON – Xoppa

答えて

1

純粋なJSONにわずかにデータを変更できる場合は、Jacksonがこれを行うことができます。あなたはJS/JSONデータに読んでいるか、私も知りませんので、私はあなたがそれを解析コードで文字列に格納されますし、管理することを前提とします:

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コードを解析できます。

関連する問題