2016-07-15 74 views
1

我得到一個JSON字符串這樣的:如何定義類來解析這個使用Gson的JSON?

{ 
    "cars": { 
    "ford": { 
     "length": 4460, 
     "weight": 1450 
    }, 
    "jeep": { 
     "length": 4670, 
     "weight": 1880 
    }, 
    "toyota": { 
     "length": 3830, 
     "weight": 1120 
    }, 
    . 
    . 
    . 
    "audi": { 
     "length": 5288, 
     "weight": 2432 
    }, 
    "subaru": { 
     "length": 4755, 
     "weight": 1790 
    }, 
    "count": 128 
    } 
} 

我嘗試定義的Java類分析使用GSON這個JSON。

public class CarSize { 
    public int length; 
    public int weight; 
} 

public class JSONData { 
    public Map<String, CarSize> cars; 
} 

問題是cars是不是一個純地圖,它具有"count":128128不是CarSize。我如何解析JSON?

請注意我無法修改JSON的源字符串。但我可以忽略「count」屬性,因爲我知道這是Map<String, CarSize> cars的大小。

回答

1

看起來您有一個Cars對象,其中包含Car List<Car>count property的列表。

問題是,count屬性在地圖中。我所做的是擴展地圖,只是爲了添加屬性。然後,你需要定義自己的串行器/解串器這個地圖 (來源get-nested-json-object-with-gson

型號:

class Cars { 
    private MyMap cars = new MyMap(); 

    public MyMap getCars() { 
     return cars; 
    } 
    public void setCars(MyMap cars) { 
     this.cars = cars; 
    } 

    public void add(String name, Car car){ 
     cars.put(name, car); 
     cars.setCount(cars.getCount()+1); 
    } 

} 

class MyMap extends HashMap<String, Car> { 
    private int count; 

    public int getCount() { 
     return count; 
    } 

    public void setCount(int count) { 
     this.count = count; 
    } 
} 

class Car { 
    private int length; 
    private int weight; 

    public Car() { 

    } 

    public Car(int length, int weight) { 
     this.length = length; 
     this.weight = weight; 
    } 

    public int getLength() { 
     return length; 
    } 

    public void setLength(int length) { 
     this.length = length; 
    } 

    public int getWeight() { 
     return weight; 
    } 

    public void setWeight(int weight) { 
     this.weight = weight; 
    } 
} 

定製串行器/解串器:

class MyDeserializer implements JsonDeserializer<MyMap> 
{ 
    @Override 
    public MyMap deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { 
     JsonElement count = json.getAsJsonObject().remove("count"); 
     MyMap myMap = new Gson().fromJson(json.getAsJsonObject().toString(), type); 
     if(count!=null){ 
      myMap.setCount(count.getAsInt()); 
     } 
     return myMap; 
    } 
} 

class MySerializer implements JsonSerializer<MyMap> 
{ 
    @Override 
    public JsonElement serialize(MyMap src, Type type, JsonSerializationContext context) { 
     JsonElement serialize = context.serialize(src, Map.class); 
     serialize.getAsJsonObject().add("count", new JsonPrimitive(src.getCount())); 
     return serialize; 
    } 
} 

代碼閱讀並寫出json:

String json = "{\"cars\":{\"jeep\":{\"length\":4670,\"weight\":1450},\"ford\":{\"length\":4460,\"weight\":1880},\"count\":128}}"; 
Cars cars = new Cars(); 
cars.add("jeep", new Car(4670, 1450)); 
cars.add("ford", new Car(4460, 1880)); 

Gson gson = new GsonBuilder() 
      .registerTypeAdapter(MyMap.class, new MyDeserializer()) 
      .registerTypeAdapter(MyMap.class, new MySerializer()) 
      .create(); 

String json2 = gson.toJson(cars); 
cars = gson.fromJson(json, Cars.class); 
cars = gson.fromJson(json2, Cars.class);   
+0

謝謝Alex。但我正在使用Gson。 Gson沒有'ObjectMapper'。 –

+0

我的不好!但它仍然激起了如何定義模型的問題。我會用Gson更新它 – alexbt

+0

對於Gson,您可以使用: Gson gson = new Gson();汽車汽車= gson.fromJson(json,Cars.class); // json是你的json字符串 –