简体   繁体   English

如何用gson解析这个问题

[英]How to parse this with gson

Here's the json data (I have no control over this) 这是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
        ]
    }
}

} }

I can't for the life of me figure out how to construct the classes to store this. 我不能为我的生活弄清楚如何构建存储它的类。 The "668" (id num) is my stumbling block. “668”(身份证号码)是我的绊脚石。

I know asking for help without showing what i've tried isn't good form, but nothing i've tried has even come close, and it would just take up lots of space to get nowhere. 我知道寻求帮助而不显示我所尝试的并不是一个好的形式,但我尝试过的任何东西都没有接近,它只会占用大量空间而无处可去。

Your class hierarchy would have the following skeleton 您的类层次结构将具有以下骨架

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;
}

You'll need classes for Metal and Leather and others. 你需要MetalLeather等课程。

The class Holder above would have been the one serialized to the JSON you've posted. 上面的类Holder将是序列化到您发布的JSON的类。 If you don't want to name your fields like base_rgb (doesn't follow convention), you can use library specific annotations like @JsonProperty (not Gson) with a name attribute to give them a JSON name and name the field whatever you want. 如果您不base_rgb那样命名您的字段(不遵循惯例),您可以使用具有name属性的库特定注释(如@JsonProperty (非Gson))为它们提供JSON名称并将字段命名为您想要的任何内容。

Gson also provides a JsonSerializer and JsonDeserializer to really customize serialization/deserialization. Gson还提供了一个JsonSerializerJsonDeserializer来真正自定义序列化/反序列化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM