Using GSON to parse array with multiple types

后端 未结 2 1246
野性不改
野性不改 2020-12-08 22:48

I wish to use GSON to parse the following json:

[
    [
        \"hello\",
        1,
        [2]
    ],
    [
        \"world\",
        3,
        [2]
             


        
2条回答
  •  心在旅途
    2020-12-08 23:18

    First, I think you may be mistaken in your example above. An Array consisting of three different is a very unusual approach, to say the least. Probably your json structure is an array, containing tuples. These tuples then include an array.

    Like:

    [
    {
        "hello",
        1,
        [2]
    },
    {
        "world",
        3,
        [2]
    }
    ]
    

    XXX should be an object containing:

    A String

    An int (or Integer)

    An Array of (I guess) ints.

    Then you make an array of these objects and parse the json into it.

    However, your json seems really badly formed, since all members should be named, like

    [
    {
        "str":"hello",
        "intVal":1,
        "intArr":[2]
    },
    {
        "str":"world",
        "intVal":3,
        "intArr":[2]
    }
    ]
    

    If, on the other hand, the JSON really looks the way you describe it, you would have to make arrays of Object, plain and simple, and then cast them when you read them from your data structure.

提交回复
热议问题