Google Gson没有正确地使用ArrayList反序列化简单对象

时间:2022-10-29 20:57:04

I am trying out Gson, but I cannot get past this issue.

我正在尝试Gson,但我无法解决这个问题。

What ends up happening is the Network class gets created (my JSON root) and three layers are added. These 3 Layers do not have the right "input" or "output" values set (both are 0).

最终发生的是创建网络类(我的JSON根)并添加了三个层。这3个图层没有设置正确的“输入”或“输出”值(均为0)。

I am expecting three layers with input and output values above 0.

我期待三层输入和输出值大于0。

Below is my JSON and the code for the classes and Gson.

下面是我的JSON以及类和Gson的代码。

Can anyone tell me what is wrong? Is it my JSON structure or Java structure?

谁能告诉我有什么问题?它是我的JSON结构还是Java结构?

Gson code:

Gson代码:

try(Reader reader = new InputStreamReader(ModelLoader.class.getResourceAsStream("/test.json"), "UTF-8")){
    Gson gson = new GsonBuilder().create();
    Network n = gson.fromJson(reader, Network.class);
    System.out.println(n);
}

I have a very very simple JSON like so (EDITED per discussion below):

我有一个非常简单的JSON就像这样(在下面的讨论中编辑):

{
  "layers" : [

    {"layer":
      {"input":300,
      "output":8000}
    },
    {"layer2":
      {"input":300,
      "output":8000}
    },
    {"layer3":
      {"input":300,
      "output":8000}
    }
  ]
}

Here is the Layer class:

这是Layer类:

public class Layer {

    int input;
    int output;
    float[][] weights;
    float[] inputs;
    float[] outputs;

    public Layer(int input, int output) {
        ...constructor code initializes vars...
    }

    public void setInput(int input) {
        this.input = input;
    }

    public void setOutput(int output) {
        this.output = output;
    }

    public void setInputs(float[] inputs) {
        this.inputs = inputs;
    }

    public void setWeight(float[][] weights) {
        this.weights = weights;
    }
}

And here is the class that contains an array list of layers: import java.util.ArrayList;

这是包含层数组列表的类:import java.util.ArrayList;

public class Network {

    ArrayList<Layer> layers;

    public Network() {
        layers = new ArrayList<Layer>();
    }

    public void addLayer(Layer l) {
        layers.add(l);
    }
}

1 个解决方案

#1


0  

Your JSON (and the respective POJO) structure should probably look like this:

您的JSON(和相应的POJO)结构应该如下所示:

    {
      "layers" : [
          {"input":300,output":8000},
          {"input":300,"output":8000},
          {"input":300,"output":8000}
      ]
    }

    public class Layer {
       int input;
       int output;
   }

    public class Network {
        ArrayList<Layer> layers;
    }

If each layer also have a name (or a label), it should have the following structure:

如果每个图层都有一个名称(或标签),则它应具有以下结构:

    {
      "layers" : [
          {"label":"layer", "input":300,output":8000},
          {"label":"layer1", "input":300,"output":8000},
          {"label":"layer2", "input":300,"output":8000}
      ]
    }

    public class Layer {
       int input;
       int output;
       String label;
   }

    public class Network {
        ArrayList<Layer> layers;
    }

#1


0  

Your JSON (and the respective POJO) structure should probably look like this:

您的JSON(和相应的POJO)结构应该如下所示:

    {
      "layers" : [
          {"input":300,output":8000},
          {"input":300,"output":8000},
          {"input":300,"output":8000}
      ]
    }

    public class Layer {
       int input;
       int output;
   }

    public class Network {
        ArrayList<Layer> layers;
    }

If each layer also have a name (or a label), it should have the following structure:

如果每个图层都有一个名称(或标签),则它应具有以下结构:

    {
      "layers" : [
          {"label":"layer", "input":300,output":8000},
          {"label":"layer1", "input":300,"output":8000},
          {"label":"layer2", "input":300,"output":8000}
      ]
    }

    public class Layer {
       int input;
       int output;
       String label;
   }

    public class Network {
        ArrayList<Layer> layers;
    }