0

I've to create from Java a JSON structure like this one:

[ [ timestamp , number ],[ timestamp , number ] ]

for using it on Highcharts graphs.

I've used a "LinkedList of LinkedList" structure that I've found formatting in json in the same way, and it works.

I wished to know if there's another way to do it. A LinkedList of LinkedList sounds weird..

EDIT:

Maybe the question wasn't clear. I'm not asking how to convert the array-of-arrays in json but WHAT to convert.

In my case I know the result of the conversion and I can choose the starting structure. Other structures, instead of "LinkedList of LinkedLists" that json-ized are like this:

[ [ x, y ] , [ z, k ] , ... ]

asked Jan 15, 2012 at 17:58

4 Answers 4

1

If u dont want to use any collection class then by simply using following code u can make the string required to u for JSON.In following code u can keep the num variable value dynamic.

StringBuilder sb = new StringBuilder();
 sb.append("[");
 int num = 5;
 for(int i=0;i<num;i++)
 {
 sb.append("[");
 sb.append(new Date().getTime() + "," + (i+1));
 sb.append("]");
 if((i+1)<num)
 sb.append(",");
 }
 sb.append("]");
answered Jan 20, 2012 at 10:29
Sign up to request clarification or add additional context in comments.

Comments

0

Another way? You haven't really said how you've already done it. I've found Json-lib easy to use and reliable, and it should turn a Java array-of-arrays into JSON for you.

answered Jan 15, 2012 at 18:02

2 Comments

I know how to made the structures in JSON (I'm using the JAX-RS because I'm building a restful web service). I'm asking which should be the starting structure instead of my LinkedList-of-LinkedLists.
Did you try an array of arrays in Java?
0

Good day!

LinkedList-of-LinkedLists is a bad way to do it, as it does not allow you to extend your structure further. You'd better take a glance at how it is implemented in existent Java-JSON wrappers. You surely need to make some class which would have encapsulated a linked list inside it. This way you could make even more complex structures without loosing readability.

answered Jan 15, 2012 at 18:23

Comments

0

You can use List of arrays:

List<long[]> hcData = new ArrayList<long[]>();
//add the pairs
hcData.add(new long[]{date.getTime(), number});

Just make sure your list (hcData) will be sorted according to the date, so that you don't have to play with it on the client side.

answered Jan 15, 2012 at 18:45

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.