Felix Martin Felix Martin - 7 months ago 24
Javascript Question

Convert this javascript array to java

I created a multi-dimensional array in JavaScript on a very old project, and I now have moved on from JS and got interested in Java. I'm developing a LibGDX game and I need this array in java format, but I'm not sure how.

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}
]
}];


How can I convert that to Java?

Note

Due to a bug with my mobile AIDE, I have to create a 1d array like this:

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];


Since it says
String[][] array = new String[5][5]
has the error "Unexpected end of declaration", so I'd prefer an answer with the format I use, if possible.

Answer

If you can modify the data slightly into pure JSON, Jackson can do this. I also don't know how you're reading in the JS/JSON data, so I'll assume that you manage to get it stored into a String in the parsing code:

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 */
}

Parsing Code

// 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
);

If you want the data left as an array, just leave out the Arrays.asList() call.

If you can't modify the code to JSON, you can use Rhino to parse old JS code.

Comments