Json到java对象映射
映射/反序列化此json的最佳解决方案是什么:Json到java对象映射,java,json,deserialization,Java,Json,Deserialization,映射/反序列化此json的最佳解决方案是什么: { "columns" : [ "name", "description", "id" ], "data" : [ [ "Train", "Train desc", 36 ], [ "Ship", "Ship desc", 35 ], [ "Plane", "Plane desc", 34 ] ] } 在该类的对象列表中: class Transport { String id; String name; String description
{ "columns" : [ "name", "description", "id" ], "data" : [ [ "Train", "Train desc", 36 ], [ "Ship", "Ship desc", 35 ], [ "Plane", "Plane desc", 34 ] ] }
在该类的对象列表中:
class Transport { String id; String name; String description; }
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();
}
}
我不知道哪些库支持JSON数组(“数据”是数组的数组)和Java对象字段之间的映射 该库允许您将JSON数组映射为java字符串数组,但随后必须将其转换为对象模型。 您可以将JSON解析为以下对象:
class DataWrapper
{
String[] columns;
String[][] data;
}
另一种解决方案是使用JSonReader,并使用以下类将对象流式输出:
class Transport { String id; String name; String description; }
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();
}
}
问题到底是什么?@dougEfresh问题是关于JSON到java的直接映射。您可以尝试使用这个:或者您使用的解析器为默认值选择的任何东西。在其他一些语言中,选择是显而易见的,但是Java对于数组和“对象”有多种选择。很好的解决方案,谢谢!