Skip to content

JSON input output in Java

aburkov edited this page Jun 27, 2015 · 1 revision

Suppose you have an xpresso dict:

dict<Integer> rank = x.dict(x.tuple("Moscow",30),x.tuple("Saint-Petersburg",15),x.tuple("New York",20),x.tuple("London",10),x.tuple("Paris",5),x.tuple("Dubai",32));

Let's first dump it as a String:

String rankAsString = x.Json(rank).toString();
x.print(rankAsString);

Console: {"New York":20,"London":10,"Saint-Petersburg":15,"Moscow":30,"Dubai":32,"Paris":5}

Now let's create a copy of the rank dict from its JSON string representation:

dict<Integer> rankCopy = x.String(rankAsString).parseJson();

Compare the original rank dict to the copy:

x.print(x.Object(rank).equals(rankCopy));

Console: true