0

I'm trying to use this library in my java project https://github.com/ralfstx/minimal-json

Here is how it's used to create objects and arrays.

JsonObject jsonObject = new JsonObject().add( "name", "John" ).add( "age", 23 );
// -> { "name": "John", "age", 23 }

JsonArray jsonArray = new JsonArray().add( "John" ).add( 23 );
// -> [ "John", 23 ]

I'm trying to create something like this

{"start":1234,"end":4321,"time":1000,"cmds":[["String",123],["String2",0],["String3",99999]]};

Here is what I tried it didn't compile

JsonObject jsonObject = new JsonObject().add("start", 1234).add("end", 848383).add("cmds", new JsonArray().add("test").add(1234), new JsonArray().add("test2").add(9594), new JsonArray().add("test6").add("down"));
System.out.println(jsonObject);

This compiles below compiles fine. But it keeps it all as one Array.

JsonObject jsonObject = new JsonObject().add("start", 1234).add("end", 848383).add("time", 1000).add("cmds", new JsonArray().add("test").add(1234).add("test2").add(9594).add("test6").add("down"));
System.out.println(jsonObject);


{"start":1234,"end":848383,"time":1000,"cmds":["test",1234,"test2",9594,"test6","down"]}
1
  • Well, you create a single array with 6 elements, so that is pretty normal Commented Mar 17, 2014 at 21:23

1 Answer 1

1

Instead of:

new JsonArray().add("test").add(1234)
    .add("test2").add(9594)
    .add("test6").add("down")

which creates one array of 6 elements, you should:

// new array,
new JsonArray()
    // add a new 2-element array in it,
    .add(new JsonArray().add("test").add(1234))
    // add a new 2-element array in it,
    .add(new JsonArray().add("test2").add(9594))
    // add a new 2-element array in it
    .add(new JsonArray().add("test6").add("down"))
Sign up to request clarification or add additional context in comments.

3 Comments

thanks it works.. I have to keep it as multiple arrays.
I don't understand what you mean?
I don't want to change the format i'm trying to emulate a server for a javascript game. Yeah I could probably change the format on both sides but your solution is perfect no need for anything else, thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.