2014-06-17 49 views
0

我用於成功地解析以下上傳.json文件:如何解析與傑克遜嵌套數組?

[ 
    { 
     "latitude": 49.419459253939316, 
     "longitude": 8.676411621072491 
    }, 
    { 
     "latitude": 49.41946061080915, 
     "longitude": 8.676411644939083 
    }, 
    { 
     "latitude": 49.420365910782735, 
     "longitude": 8.676438042403413 
    } 
] 

以下Jackson script輸出點的List

private static <T> List<T> parseFile(final String fileName, 
            Class<T> contentType) { 
    // ... 
    InputStream inputStream = // Open file 
    ObjectMapper objectMapper = new ObjectMapper(); 
    try { 
     TypeFactory typeFactory = objectMapper.getTypeFactory(); 
     CollectionType collectionType = typeFactory 
      .constructCollectionType(List.class, contentType); 
     return objectMapper.readValue(inputStream, collectionType); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 

現在數據集變得更加複雜。點的List變成點的ListList。 我這樣構造它 - 如果這是不正確的請糾正我。

[ 
    [ 
     { 
      "latitude": 49.419459253939316, 
      "longitude": 8.676411621072491 
     }, 
     { 
      "latitude": 49.41946061080915, 
      "longitude": 8.676411644939083 
     }, 
     { 
      "latitude": 49.420365910782735, 
      "longitude": 8.676438042403413 
     } 
    ], 
    [ 
     { 
      "latitude": 49.40460334213399, 
      "longitude": 8.670034018853409 
     }, 
     { 
      "latitude": 49.404608057285145, 
      "longitude": 8.670028775634165 
     }, 
     { 
      "latitude": 49.40506145685422, 
      "longitude": 8.66955817506422 
     } 
    ] 
] 

我準備了以下的POJOs將數據存儲到:

public class GeoPoint { 
    public double latitude; 
    public double longitude; 
} 

...

public class ThreePoints { 
    public List<GeoPoint> points; 
} 

怎麼辦我必須要改變上述傑克遜解析器,因此它可以處理嵌套數組?傑克遜可以將數據解析爲一個嵌套的類結構,例如ThreePoints.class

回答

1

你可以寫一個簡單的自定義解串器

要將其反序列化到您的類:

public class GeoPoint { 
    public double latitude; 
    public double longitude; 
} 

public class ThreePoints { 
    public List<GeoPoint> points; 
} 

編寫自定義解串器:

class ThreePointsDeserializer extends StdDeserializer<ThreePoints> { 

    protected ThreePointsDeserializer() { 
     super(ThreePoints.class); 
    } 


    @Override 
    public ThreePoints deserialize(JsonParser parser, DeserializationContext ctx) 
      throws IOException, JsonProcessingException { 
     ThreePoints result = new ThreePoints(); 
     GeoPoint[] points = parser.getCodec().readValue(parser, GeoPoint[].class); 
     result.points = Arrays.asList(points); 
     return result; 
    } 
} 

使用該解串器:

SimpleModule module = new SimpleModule(); 
module.addDeserializer(ThreePoints.class, new ThreePointsDeserializer()); 

ObjectMapper mapper = new ObjectMapper(); 
mapper.registerModule(module); 

TypeFactory tf = mapper.getTypeFactory(); 
CollectionType collectionType = tf.constructCollectionType(List.class, ThreePoints.class); 

List<ThreePoints> result = mapper.readValue(YOUR_DATA, collectionType); 
2

您可以簡單地創建額外的集合類型。請參見下面的代碼:

TypeFactory typeFactory = mapper.getTypeFactory(); 
CollectionType listType = typeFactory.constructCollectionType(List.class, contentType); 
CollectionType listListType = typeFactory.constructCollectionType(List.class, listType); 
List<List<GeoPoint>> readValue = mapper.readValue(json, rootCollectionType); 
// convert to ThreePoints 

編輯
不幸的是,你不能告訴JacksonJSONPOJO類進行轉換,因爲他們不適合對方。 Jackson也不包含可用於將JSON映射到POJO類的註釋。你必須手動完成。使用我的代碼,您可以編寫ThreePoints類的定製解串器,您可以在其中使用內部ObjectMapper和我的上述代碼。我認爲,你將能夠輕鬆地將List<List<GeoPoint>> readValue轉換爲ThreePoints類。另一種選擇 - 你可以在你的類裏寫一個簡單的函數JsonUtil。如果你真的不能改變這個JSON你必須手動完成。

+0

你可以嗎租賃更新您的解決方案,以顯示如何生成一個'List '? – JJD

0

您需要先讀取Matrix的值, 首先需要使用名爲Place的Pojo來映射值。

public class Place { 

    double latitude; 
    double longitude; 

    public double getLatitude() { 
     return latitude; 
    } 

    public void setLatitude(double latitude) { 
     this.latitude = latitude; 
    } 

    public double getLongitude() { 
     return longitude; 
    } 

    public void setLongitude(double longitude) { 
     this.longitude = longitude; 
    } 
}  

第二步,您需要將JsonNode映射到Matrix。

import java.io.IOException; 
import java.util.List; 
import org.codehaus.jackson.JsonNode; 
import org.codehaus.jackson.map.ObjectMapper; 
import org.codehaus.jackson.map.type.CollectionType; 
import org.codehaus.jackson.map.type.TypeFactory; 


public class Main { 
    static String jsonString = "[" + 
" [" + 
"  {" + 
"   \"latitude\": 49.419459253939316," + 
"   \"longitude\": 8.676411621072491" + 
"  }," + 
"  {" + 
"   \"latitude\": 49.41946061080915," + 
"   \"longitude\": 8.676411644939083" + 
"  }," + 
"  {" + 
"   \"latitude\": 49.420365910782735," + 
"   \"longitude\": 8.676438042403413" + 
"  }" + 
" ]," + 
" [" + 
"  {" + 
"   \"latitude\": 49.40460334213399," + 
"   \"longitude\": 8.670034018853409" + 
"  }," + 
"  {" + 
"   \"latitude\": 49.404608057285145," + 
"   \"longitude\": 8.670028775634165" + 
"  }," + 
"  {" + 
"   \"latitude\": 49.40506145685422," + 
"   \"longitude\": 8.66955817506422" + 
"  }" + 
" ]" + 
"]"; 

    public static void main(String...args) { 
     final ObjectMapper om = new ObjectMapper(); 
     try { 
      final Place[][] dtos = om.readValue(new ObjectMapper() 
       .readValue(jsonString, JsonNode.class), Place[][].class); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

乾杯。

+0

我想將它解析成'List'。 – JJD