Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

What is the best solution for mapping/deserialize this json:

 { "columns" : [ "name", "description", "id" ], "data" : [ [ "Train", "Train desc", 36 ], [ "Ship", "Ship desc", 35 ], [ "Plane", "Plane desc", 34 ] ] } 

in to a list of objects of this class:

class Transport { String id; String name; String description; }
share|improve this question
 
What exactly is the question? –  dougEfresh Jul 10 '13 at 18:17
 
@dougEfresh The question is about JSON straightforward mapping to the java. –  Roman C Jul 10 '13 at 18:20
1  
you can try using this : json.org/java or code.google.com/p/google-gson –  Mehdi Karamosly Jul 10 '13 at 18:22
 
Whatever the parser you use chooses for the defaults. In some other languages the choices are obvious, but Java has several choices each for arrays and "objects". –  Hot Licks Jul 10 '13 at 18:29
add comment

closed as unclear what you're asking by zacheusz, Raedwald, Eran, djf, mguymon Jul 10 '13 at 19:57

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

up vote 1 down vote accepted

I don't know libraries that supports mapping between JSON Arrays ("data" is an array of arrays) and Java object fields.

The gson library let you map your JSON Array into an array of array of java String, but then you have to convert it to your object model. You can parse your JSON into this object:

class DataWrapper
{
    String[] columns;
    String[][] data;
}

Another solution is to use the JSonReader and stream out your objects using this class:

import java.io.IOException;
import java.io.Reader;
import java.util.Iterator;

import com.google.gson.stream.JsonReader;

public class TransportJSonReader implements Iterator<Transport> {

protected JsonReader jsonReader;

public TransportJSonReader(Reader reader) throws IOException
{
    jsonReader = new JsonReader(reader);
    jsonReader.beginObject();

    //columns
    jsonReader.nextName();
    jsonReader.skipValue();

    //data
    jsonReader.nextName();
    jsonReader.beginArray();

}

@Override
public boolean hasNext() {
    try {
        return jsonReader.hasNext();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

@Override
public Transport next() {
    if (!hasNext()) throw new IllegalStateException();

    try {
        jsonReader.beginArray();
        String name = jsonReader.nextString();
        String description = jsonReader.nextString();
        String id = jsonReader.nextString();
        jsonReader.endArray();
        return new Transport(id, name, description);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

@Override
public void remove() {
    throw new UnsupportedOperationException();
}

}

It is an iterator so you can use it in this way:

    TransportJSonReader reader = new TransportJSonReader(new StringReader(json));
    while(reader.hasNext()) System.out.println(reader.next());
share|improve this answer
 
good solution, thanks! –  mgramin Jul 11 '13 at 16:51
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.