0

这是json数据(我无法控制)

{
"colors": {
    "668": {
        "name": "Pink Ice",
        "base_rgb": [
            128,
            26,
            26
        ],
        "cloth": {
            "brightness": 50,
            "contrast": 1.36719,
            "hue": 8,
            "saturation": 0.351563,
            "lightness": 1.36719,
            "rgb": [
                216,
                172,
                164
            ]
        },
        "leather": {
            "brightness": 47,
            "contrast": 1.71875,
            "hue": 8,
            "saturation": 0.234375,
            "lightness": 1.71875,
            "rgb": [
                207,
                170,
                163
            ]
        },
        "metal": {
            "brightness": 47,
            "contrast": 1.64063,
            "hue": 8,
            "saturation": 0.429688,
            "lightness": 1.48438,
            "rgb": [
                211,
                145,
                134
            ]
        }
    }
},
"657": {
    "name": "Pastel Pink",
    "base_rgb": [
        128,
        26,
        26
    ],
    "cloth": {
        "brightness": 52,
        "contrast": 1.40625,
        "hue": 8,
        "saturation": 0.585938,
        "lightness": 1.40625,
        "rgb": [
            247,
            170,
            157
        ]
    },
    "leather": {
        "brightness": 52,
        "contrast": 1.40625,
        "hue": 8,
        "saturation": 0.546875,
        "lightness": 1.40625,
        "rgb": [
            243,
            172,
            159
        ]
    },
    "metal": {
        "brightness": 47,
        "contrast": 1.5625,
        "hue": 8,
        "saturation": 0.546875,
        "lightness": 1.40625,
        "rgb": [
            220,
            141,
            126
        ]
    }
}

}

我一生都无法弄清楚如何构建类来存储它。“668”(id num)是我的绊脚石。

我知道在不展示我尝试过的东西的情况下寻求帮助不是很好的形式,但是我尝试过的任何东西都没有接近,而且它只会占用大量空间而无处可去。

4

1 回答 1

2

您的类层次结构将具有以下骨架

public class Holder {
    private Colors colors;
}

public class Colors {
    private Map<String, Item> map;
}

public class Item {
    private String name;
    private int[] base_rgb;
    private Cloth cloth;
    ... // more
}

public class Cloth {
    private int brightness;
    private float contrast;
    private int hue;
    private float saturation;
    private float lightness;
    private int[] rgb;
}

您将需要MetalLeather其他人的课程。

上面的类Holder将是序列化为您发布的 JSON 的类。如果您不想将字段命名为base_rgb(不遵循约定),您可以使用库特定的注释(例如@JsonProperty(不是 Gson)和name属性来为它们提供 JSON 名称并根据需要命名字段。

Gson 还提供了一个JsonSerializerJsonDeserializer来真正自定义序列化/反序列化。

于 2013-09-06T15:28:31.777 回答