2016-04-27 122 views
-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]有错误“申报意外结束” ,所以如果可能的话,我更喜欢用我使用的格式的答案。

+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代码。