我正在尝试Gson,但我无法解决这个问题。
最终发生的是创建了Network类(我的JSON root)并添加了三个层。这3个图层没有设置正确的“输入”或“输出”值(均为0)。
我期待三层输入和输出值大于0。
下面是我的JSON以及类和Gson的代码。
谁能告诉我有什么问题?它是我的JSON结构还是Java结构?
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);
}
我有一个非常简单的JSON,如(在下面的讨论中编辑):
{
"layers" : [
{"layer":
{"input":300,
"output":8000}
},
{"layer2":
{"input":300,
"output":8000}
},
{"layer3":
{"input":300,
"output":8000}
}
]
}
这是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;
}
}
这是包含图层数组列表的类: import java.util.ArrayList;
public class Network {
ArrayList<Layer> layers;
public Network() {
layers = new ArrayList<Layer>();
}
public void addLayer(Layer l) {
layers.add(l);
}
}
答案 0 :(得分:0)
您的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;
}
如果每个图层都有一个名称(或标签),则它应具有以下结构:
{
"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;
}