0

I have a class with some fields:

public class GsonRepro {
    class A {
        private List<String> field1 = new ArrayList<>();
        private String name;
        private Integer status;

        public A(){
        }

        public List<String> getfield1() { return field1; }
        public void setField1(List<String> field1) { this.field1 = field1; }

        public String getName() { return name; }
        public void setName() { this.name = name; }

        public Integer getStatus() { return status; }
        public void setStatus(int status) { this.status = status; }
    }

    public static void main(String[] args) {
        String str = "{\"name\":\"my-name-1\",\"status\":0,\"field1\":[\"0eac6b1d3d494c2d8568cd82d9d13d5f\"]}";
        A a = new Gson().fromJson(str, A.class);
    }
}

All fields are parsed but the List<String> field1, how can I get this to work?

Solution:

The code above works just fine. Initially, I just had a typo in the List field.

Joe Almore
  • 4,036
  • 9
  • 52
  • 77
  • Can you include your json String? – f1sh Jan 10 '22 at 14:59
  • @f1sh, Yes, post has been updated. – Joe Almore Jan 10 '22 at 15:03
  • 1
    i tried to reproduce and could not https://pastebin.com/D5fE06iB – scigs Jan 10 '22 at 15:13
  • 2
    I alco cannot reproduce this. Running `System.out.println(a.field1);` prints `[0eac6b1d3d494c2d8568cd82d9d13d5f]`. Do you have a typo somewhere? Does your `str` really contain a `field1` element? – f1sh Jan 10 '22 at 15:14
  • @f1sh not sure what you mean. but `str={"name":"my-name-1","status":0,"field1":["0eac6b1d3d494c2d8568cd82d9d13d5f"]}` there you can see a `field1` – Joe Almore Jan 10 '22 at 15:25

3 Answers3

0

you can try to use TypeToken like in this answer to another question.

For you it would look like this:

import java.lang.reflect.Type;
import com.google.gson.reflect.TypeToken;

...

Type type = new TypeToken<A>(){}.getType();
A a = new Gson().fromJson(str, type);

Greetings

JuKe
  • 39
  • 1
  • 2
0

I tried with the code as above that you shared and is working fine without any issues. Please check following code and verify,

public static void main(String[] args) {
    String str = "{\"name\":\"my-name-1\",\"status\":0,\"field1\":[\"0eac6b1d3d494c2d8568cd82d9d13d5f\"]}";
    A a = new Gson().fromJson(str, A.class);
    System.out.println(a.getName());
    System.out.println(a.getStatus());
    System.out.println(a.getfield1());
}

Following is the output which is being printed on console as,

my-name-1
0
[0eac6b1d3d494c2d8568cd82d9d13d5f]
0
import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;

class A {
    private List<String> field1 = new ArrayList<>();
    private String name;
    private Integer status;

    public A() {
    }

    public List<String> getfield1() {
        return field1;
    }

    public void setField1(List<String> field1) {
        this.field1 = field1;
    }

    public String getName() {
        return name;
    }

    public void setName() {
        this.name = name;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }
}

public class GsonParseList {
    public static void main(String[] args) {
        String str = "{'name':'my-name-1','status':0,'field1':['0eac6b1d3d494c2','d8568cd82d9d13d5f']}";
        A a = new Gson().fromJson(str, A.class);
        System.out.println(a.getfield1());
    }
}