简体   繁体   English

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

[英]Google Gson not deserializing simple object with ArrayList properly

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. 最终发生的事情是创建了Network类(我的JSON根),并添加了三层。 These 3 Layers do not have the right "input" or "output" values set (both are 0). 这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);
    }
}

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

错误的反序列化列表<object>与 gson<div id="text_translate"><p> 我用List&lt;Object&gt;调试CUSTOMER object:</p><p> <a href="https://i.stack.imgur.com/Gh2Mx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Gh2Mx.png" alt="在此处输入图像描述"></a></p><p> 这是 CUSTOMER.class:</p><pre> public class CUSTOMER { @XmlElements({ @XmlElement(name = "ADDRESS", type = ADDRESS.class), @XmlElement(name = "ZIP", type = ZIP.class), @XmlElement(name = "CITY", type = CITY.class), @XmlElement(name = "COUNTRY", type = COUNTRY.class), }) protected List&lt;Object&gt; addressAndZIPAndCITY; // other fields }</pre><p> 但是当我反序列化并从中创建 json 时,它仅包含:</p><pre> { "addressAndZIPAndCITY": [ { "value": "some value", "type": "some type" }, { "value": "some value 2", "type": "some type 2" }] }</pre><p> 缺少 ADRESS、ZIP、CITY 和 COUNTRY 对象标题。 所以反序列化不好。</p><p> 我无法更改List&lt;Object&gt;声明。 是否可以选择将其反序列化为带有 ADRESS、ZIP、CITY 等的 json? 像这样:</p><pre> { "addressAndZIPAndCITY": [{ "ADDRESS": { "value": "some value", "type": "some type" } }, { "ZIP": { "value": "some value 2", "type": "some type 2" } } ] }</pre><p> 编辑:我的意思是它看起来像 GSON 不知道哪个 object 在那里,但可能你知道如何向 model 添加一些注释或其他东西来识别它?</p></div></object> - Wrong deserializing List<Object> with gson

暂无
暂无

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

相关问题 Google Gson 反序列化问题 - Google Gson issue with deserializing 使用GSON序列化/反序列化简单对象-无时区指示符错误 - Serializing/Deserializing simple object using GSON - No time zone indicator error gson无法反序列化简单界面 - gson fails in deserializing simple interface 反序列化地图 <Object, Object> 与GSon - Deserializing Map<Object, Object> with GSon 错误的反序列化列表<object>与 gson<div id="text_translate"><p> 我用List&lt;Object&gt;调试CUSTOMER object:</p><p> <a href="https://i.stack.imgur.com/Gh2Mx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Gh2Mx.png" alt="在此处输入图像描述"></a></p><p> 这是 CUSTOMER.class:</p><pre> public class CUSTOMER { @XmlElements({ @XmlElement(name = "ADDRESS", type = ADDRESS.class), @XmlElement(name = "ZIP", type = ZIP.class), @XmlElement(name = "CITY", type = CITY.class), @XmlElement(name = "COUNTRY", type = COUNTRY.class), }) protected List&lt;Object&gt; addressAndZIPAndCITY; // other fields }</pre><p> 但是当我反序列化并从中创建 json 时,它仅包含:</p><pre> { "addressAndZIPAndCITY": [ { "value": "some value", "type": "some type" }, { "value": "some value 2", "type": "some type 2" }] }</pre><p> 缺少 ADRESS、ZIP、CITY 和 COUNTRY 对象标题。 所以反序列化不好。</p><p> 我无法更改List&lt;Object&gt;声明。 是否可以选择将其反序列化为带有 ADRESS、ZIP、CITY 等的 json? 像这样:</p><pre> { "addressAndZIPAndCITY": [{ "ADDRESS": { "value": "some value", "type": "some type" } }, { "ZIP": { "value": "some value 2", "type": "some type 2" } } ] }</pre><p> 编辑:我的意思是它看起来像 GSON 不知道哪个 object 在那里,但可能你知道如何向 model 添加一些注释或其他东西来识别它?</p></div></object> - Wrong deserializing List<Object> with gson Gson重建反序列化对象时丢失数据 - Gson losing data when reconstructing deserializing object 使用GSON和RETROFIT反序列化复杂的JSON对象 - Deserializing complex JSON object with GSON and RETROFIT 使用GSON反序列化包含JSON的对象 - Deserializing an object that contains JSON using GSON Gson 反序列化对象时忽略 null - Gson ignore null when deserializing object GSON JsonSyntaxException反序列化Java Date对象 - GSON JsonSyntaxException deserializing Java Date Object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM